how to break async.js each loop?

20,298

Solution 1

async.until repeatedly calls function until the test returns true. So test must return true so that you exit the loop. This is opposite of async.whilst which runs repeatedly while test evaluates to be true.

async.each calls the functions in parallel so what it returns does not matter. It is not a loop which you can break, but an iterator looping over the array. Your condition to stop using async.each should be in test for async.until and you should iterate over the rules yourself.

Solution 2

There isn't really a "loop" as such to break out of. All your items in your collection are used in parallel

The only way to "break" the "loop" is to call the callback with an error argument. As there is nothing to stop you from putting other things in there you could hack it a little bit.

From the docs:

Note, that since this function applies the iterator to each item in parallel there is no guarantee that the iterator functions will complete in order.

Even if you return an error, you will still have several outstanding requests potentially so you really want to limit the amount of items you use in one go. To limit the amount of outstanding requests, you could use eachSeries or eachLimit.

For example:

    async.each(_rules, 
        function(rule,callback) {
            if(changes){                       
                  return callback({ data: 'hi'}); // stop
            } 
            ...
            if(realerror){
               return callback({ error: realerror}); // stop with an error
            }
            callback(); // continue
        }, 

        function(result){
          if(!result){
            return othercallback('no results');
          }
          // check if we have a real error:
          if(result.error){
            return othercallback(result.error);
          }
          return othercallback(null, result.data);
        }
    );

PS: if you're not doing async, use underscore

Share:
20,298
Mithun Satheesh
Author by

Mithun Satheesh

"I'm a dog chasing cars. I wouldn't know what to do with one if I caught it! You know, I just.... DO things."

Updated on July 30, 2022

Comments

  • Mithun Satheesh
    Mithun Satheesh almost 2 years

    Hi i am using async module of node.js for implementing a for loop asynchronously.

    My question is: how to break the loop execution and get out of the loop? I tried giving return , return false but no luck.

    Here is the sample code:

    async.until( 
    
        function() { return goal; },
    
        function(callback) {
    
            async.each(_rules, 
    
                function(rule,callback) {
    
                    var outcome = true;
                    .... some code ....
                    if(changes){ 
                          console.log("hi");
                          return false;// HERE I NEED TO BREAK
                    } 
                    else
                    callback();
    
                }, 
    
                function(err){  }
            );
    
            if (!changes || session.process)                
                goal = true;
    
            callback();
        },
    
        function(err){ callback(session); }     
    );
    
  • Mithun Satheesh
    Mithun Satheesh about 11 years
    could you tell me how to do this here? i cant call callback(1) at the place of return false; line i have shown in the code and break it
  • Chad
    Chad about 11 years
    @mithunsatheesh Short answer: you have to, and yes you can.
  • AndyD
    AndyD about 11 years
    instead of return false, just do "return callback({data:false})" and check the first argument in the final callback.
  • Jujhar Singh
    Jujhar Singh about 11 years
    Didn't fix my issue as such, but inspired me to try a different approach.