简体   繁体   中英

How to break out of async.js map's iterator loop when a condition is met

Here date is an array. When if condition satisfies it has to break out of the iterator. But because of iterator it continues looping. Once again it executes if condition and throws the error "can't set header after they are sent". I have to break out of iterator once it executes if condition.

validatemultiday: function(req, callback) {
  async.map(edate, function iterator(date, mapCb) {
    var rangedate = moment(date).format("YYYY-MM-DD");
    if (rangedate == studentfromdate || rangedate == studenttodate) {
      sails.log.info("does1");
      callback(null, 1);
    } else {
      sails.log.info("do2");
      mapCb(null, 2);
    }
  }, callback);
}

It returns only false if if-condition is executed. It is working.

validatemultiday: function(req,callback){
                            var range = true;
                            async.map(edate, function iterator (date, mapCb){
                            var rangedate = moment(date).format("YYYY-MM-DD");
                            if(rangedate==studentfromdate||rangedate==studenttodate){
                                sails.log.info("does2");
                                range=false;
                                mapCb(null,range);
                            }
                            else{
                                sails.log.info("did2");
                                if(range==false){
                                    mapCb(null,range);
                                }
                                else{
                                    range=true;
                                    sails.log.info('2:'+range);
                                    mapCb(null,range);
                                }
                            }
                            });
                            setTimeout(function(){
                            callback(null,range);
                            },100);
                            }

调用callback()或使用break语句。

try break; or return; when you want the loop to stop.

_.some or async.some will be useful here.

Use this if you want to do some async operation with each date in edate

validatemultiday: function(req, callback) {
    async.some(edate, function iterator(date, someCb) {
        var rangedate = moment(date).format('YYYY-MM-DD');
        var isOnRange = (rangedate == studentfromdate || rangedate == studenttodate);
        // sample async operation: fs.exists
        return fs.exist(rangedate, someCb);
    }, function(result) {
        sails.log.info('validatemultiday', 'result', result);
        return callback(null, result ? 1 : 2);
    });
}

Use this if there is only sync check with each date in edate

validatemultiday: function(req, callback) {
    var result = _.some(edate, function(date) {
        var rangedate = moment(date).format('YYYY-MM-DD');
        var isOnRange = (rangedate == studentfromdate || rangedate == studenttodate);
        return isOnRange;
    });

    return callback(null, result ? 1 : 2);
    // Zalgo alert: Ideally below return statement should be used in place of above one because above code is synchronous to contain Zalgo.
    // return async.nextTick(function() {
    //     return callback(null, result ? 1 : 2);
    // });
}

Zalgo : Skip Zalgo stuff, if you are just starting with asynchronous programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM