0

Regarding the code below, my goal is to break out of FOR LOOP B and continue with FOR LOOP A, but within a callback function.

for(var a of arrA) {
    // ...
    // ...

    for(var b of arrB) {
        // ...
        // ...

        PartService.getPart(a.id, function(err, returnObj) {
            break;
        });
    }
}

Will this give me the results I want? If not, how can I achieve this?


EDIT 4/28/16 3:28 MST

  • The callback function is indeed asynchronous

Based on one of the answers and all the comments below, without changing the scope of the question, perhaps the best question at this point is "How do I implement a synchronous callback function in Node.js?". I am considering refactoring my code so to remove for loops, but I am still curious if I can still go in this direction using synchronous callback functions. I know that Underscore.js uses synchronous callback functions, but I do not know how to implement them.

Brian
  • 310
  • 1
  • 4
  • 13
  • 1
    "Will this give me the results I want?" — Have you tried it? – Quentin Apr 28 '16 at 18:14
  • 2
    Why does `PartService.getPart` take a callback in the first place? Is it asynchronous? – Quentin Apr 28 '16 at 18:14
  • 3
    `break` can only be inside a loop. The callback introduces a new boundary so it is not considered inside the `for` loop. Also, both loops will have run to completion before the callback is executed (assuming it is async). Loops are synchronous, so they don't play well will asynchronous code (in their bodies). It would probably be useful for you to read more about asynchronous code flow in JavaScript first. – Felix Kling Apr 28 '16 at 18:16
  • Thanks Felix Kling, yes it is asynchronous. I have never heard of synchronous callback functions. I am actually trying to convert PHP code into JavaScript and it seems I will need to put a little more thought into it. – Brian Apr 28 '16 at 21:14
  • 1
    This `break` should give you a runtime error, or just be ignored, because it is not in the loop. – Thevs Apr 29 '16 at 06:20

3 Answers3

2

You can try something like this, however it will work only when the callback is fired synchronously.

for(var a of arrA) {
  let shouldBreak = false;
  for(var b of arrB) {
    if (shouldBreak)
      break;
   // rest of code
     PartService.getPart(a.id, function(err, returnObj) { // when the callback is called immediately it will work, if it's called later, it's unlikely to work
       shouldBreak = true;
    });
Jakub Rożek
  • 2,110
  • 11
  • 12
  • 1
    Why the function needs callback, if it does not async? – alexmac Apr 28 '16 at 18:18
  • 2
    @Alexander: `.sort`, `.map`, `.forEach`, `.filter` all take "callbacks" but are not async. But yeah, the naming / structure of this function seem to indicate that it is async. – Felix Kling Apr 28 '16 at 18:19
  • @FelixKling yes, you're right, I'm not quite correctly formulated my question. – alexmac Apr 28 '16 at 18:23
  • @AlexanderMac, as the Felix pointed out, the function doesn't have to be asynchronous, but in the world of NodeJS, most callbacks stand for async stuff. However I only posted the idea, and marked it is possible only in case it is sync. – Jakub Rożek Apr 28 '16 at 18:26
  • If I wanted to stick with the looping, how would I go about making the callback synchronous? That seems to be the best question without going being the scope of the original question. – Brian Apr 28 '16 at 21:27
0

This might not give you expected result . You can use events EventEmitter or async module .

Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80
0

You can also try having it loop by itself without a for loop. Making it asynchronous.

for (var a of arrA) {
    var _loop = function(i) {

        // Make modifications to arrB
        
        PartService.getPart(id, function(err, retObj) {
            if (err) {
                throw err;
            }

            if (retObj.success) {
                // If successful, call function again
                _loop(i + 1);
            } else {
                // We've broken out of the loop
            }
        });
    };
    // Instantiate loop
    _loop(0);
}
SeaWorld
  • 352
  • 3
  • 11