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.

5
  • 1
    "Will this give me the results I want?" — Have you tried it? Commented Apr 28, 2016 at 18:14
  • 2
    Why does PartService.getPart take a callback in the first place? Is it asynchronous? Commented Apr 28, 2016 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. Commented Apr 28, 2016 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. Commented Apr 28, 2016 at 21:14
  • 1
    This break should give you a runtime error, or just be ignored, because it is not in the loop. Commented Apr 29, 2016 at 6:20

2 Answers 2

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;
    });
Sign up to request clarification or add additional context in comments.

5 Comments

Why the function needs callback, if it does not async?
@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.
@FelixKling yes, you're right, I'm not quite correctly formulated my question.
@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.
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.