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.
PartService.getParttake a callback in the first place? Is it asynchronous?breakcan only be inside a loop. The callback introduces a new boundary so it is not considered inside theforloop. 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.breakshould give you a runtime error, or just be ignored, because it is not in the loop.