Is there a way to stop execution of next function of series with async in nodejs?

前端 未结 4 1015
深忆病人
深忆病人 2021-02-14 06:13
  async.map(list, function(object, callback) {
    async.series([
      function(callback) {
        console.log(\"1\");

        var booltest = false;
        // assumi         


        
4条回答
  •  攒了一身酷
    2021-02-14 06:55

    To make it logical, you could just rename error to something like errorOrStop:

    var test = [1,2,3];
    
    test.forEach( function(value) {
        async.series([
            function(callback){ something1(i, callback) },
            function(callback){ something2(i, callback) }
        ],
        function(errorOrStop) {
            if (errorOrStop) {
                if (errorOrStop instanceof Error) throw errorOrStop;
                else return;  // stops async for this index of `test`
            }
            console.log("done!");
        });
    });
    
    function something1(i, callback) {
        var stop = i<2;
        callback(stop);
    }
    
    function something2(i, callback) {
        var error = (i>2) ? new Error("poof") : null;
        callback(error);
    }
    

提交回复
热议问题