How to stop executing waterfall on error in async of node.js?

僤鯓⒐⒋嵵緔 提交于 2020-01-02 13:42:06

问题


I am using async module with waterfall method.

async.waterfall([
        function(callback) {
            ...
            callback(err);
        },
        function(result, callback) {
            console.log("This function should not be executed");
        }
    ],
    function(err) {
        if (err) {
            next(err);
            return;
        }
    }
);

But the second function always execute. How to prevent it?


回答1:


Try adding a return

async.waterfall([
        function(callback) {
            ...
            return callback(err); //note return here
        },
        function(result, callback) {
            console.log("This function should not be executed");
        }
    ],
    function(err) {
        if (err) {
            next(err);
            return;
        }
    }
);


来源:https://stackoverflow.com/questions/32559822/how-to-stop-executing-waterfall-on-error-in-async-of-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!