最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to break async.js each loop? - Stack Overflow

programmeradmin0浏览0评论

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); }     
);

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); }     
);
Share Improve this question edited Apr 23, 2013 at 13:43 Jujhar Singh 3,7705 gold badges30 silver badges39 bronze badges asked Feb 18, 2013 at 14:52 Mithun SatheeshMithun Satheesh 27.9k14 gold badges78 silver badges101 bronze badges 3
  • 3 you can break by sending something in the first parameter of the callback (which async will interpret as an "error" and stop the loop). Make it a known value (a truthy one) that you can check for in your final callback to know it was a break and not an error. – Chad Commented Feb 18, 2013 at 14:54
  • 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 – Mithun Satheesh Commented Feb 18, 2013 at 15:11
  • If you don't call the callback in all paths, then the loop will never "end". If you have a path not calling the callback, then you are doing it wrong. This is asynchronous, your return value does nothing. – Chad Commented Feb 18, 2013 at 15:21
Add a comment  | 

3 Answers 3

Reset to default 7

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.

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

You have also async.detect

Returns the first value in coll that passes an async truth test. The iteratee is applied in parallel, meaning the first iteratee to return true will fire the detect callback with that result.

// asynchronous function that checks if a file exists
function fileExists(file, callback) {
   fs.access(file, fs.constants.F_OK, (err) => {
       callback(null, !err);
   });
}

async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists,
   function(err, result) {
       console.log(result);
       // dir1/file1.txt
       // result now equals the first file in the list that exists
   }
);

发布评论

评论列表(0)

  1. 暂无评论