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

javascript - The "err" argument when using Async waterfall in node.js - Stack Overflow

programmeradmin0浏览0评论

I am trying to execute a series of functions, each passing the callback to the next. Right now it looks like this (excuse any minor errors, I am rewriting it as I post!):

function func1(callback) {
    callback(null, "stuff");
}

function func2(input, callback) {
    callback(null, "foo" + input);
}

async.waterfall([func1, func2], function(err, result) {
    sys.puts(result);
});

My first question is that I'm not sure how to start this function gracefully, since it can't take an input. I'm going to eventually wrap that one in a local function, but it still makes me slightly uneasy.

Secondly, while this works, I have no idea how the "err" argument plays into this. If I try to insert it into the list of arguments, it breaks in various ways. I'd like to be able to catch an error in any function individually - or is this needed, since I have an error on the last callback which is passed down?

I am trying to execute a series of functions, each passing the callback to the next. Right now it looks like this (excuse any minor errors, I am rewriting it as I post!):

function func1(callback) {
    callback(null, "stuff");
}

function func2(input, callback) {
    callback(null, "foo" + input);
}

async.waterfall([func1, func2], function(err, result) {
    sys.puts(result);
});

My first question is that I'm not sure how to start this function gracefully, since it can't take an input. I'm going to eventually wrap that one in a local function, but it still makes me slightly uneasy.

Secondly, while this works, I have no idea how the "err" argument plays into this. If I try to insert it into the list of arguments, it breaks in various ways. I'd like to be able to catch an error in any function individually - or is this needed, since I have an error on the last callback which is passed down?

Share Improve this question edited Feb 11, 2013 at 14:54 hippietrail 17k21 gold badges109 silver badges178 bronze badges asked Jan 6, 2011 at 18:09 V_HV_H 1,7933 gold badges34 silver badges59 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

Waterfall is normally used with anonymous functions, therefore the parameters come from the outer scope.

How the err works is simple, when you supply anything that evaluates to true as the first argument of the callback function waterfall will stop and call the main callback.

function doStuff(foo, bla) {
    // more setup here

    async.waterfall([
        function(callback){
            try {
                // something that might explode
                callback(null, foo, bla);

            } catch (e) {
                callback(e);
            }
        },

        function(arg1, arg2, callback){
            callback(null, 'three');
        },

        function(arg1, callback){
            callback(null, 'done');
        }
    ],

    function (err, status) {
       // if the above try/catche catches something, we will end up here
       // otherwise we will receive 'done' as the value of status
       // after the third function has finished
    });
}

You can use async.constant to pass an input to the first function in the chain:

async.waterfall([
    async.constant(parameter),
    func1,
    func2
 ], function (err, result) {            
 });

and modify the first function of course:

function func1(input,callback) {
    callback(null, "stuff");
}

Optional: you can call callback with a non-null first parameter:

 function func1(input,callback) {
        callback("something");
    }

In this case the chain is stopped, and the last function will be called with err="something"

Documentation: https://caolan.github.io/async/docs.html#constant and https://caolan.github.io/async/docs.html#waterfall

发布评论

评论列表(0)

  1. 暂无评论