Reputation: 310
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
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.
Upvotes: 0
Views: 2767
Reputation: 2130
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;
});
Upvotes: 2
Reputation: 12945
This might not give you expected result . You can use events EventEmitter or async module .
Upvotes: 0