0
async.waterfall([1,2,3,4].map(function (arrayItem) {
        return function (lastItemResult, nextCallback) {
            // same execution for each item in the array
            var itemResult = (arrayItem+lastItemResult);
            // results carried along from each to the next
            nextCallback(null, itemResult);
        }}), function (err, result) {
        // final callback
    });

所以我是异步的新手并尝试了一个简单的示例,但收到此错误,此方法有什么问题 TypeError: nextCallback is not a function

上面的代码有什么问题?

4

2 回答 2

0

如果您查看异步文档,您会注意到以下示例async.waterfall

async.waterfall([
    function(callback) {
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback) {
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function(arg1, callback) {
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
    // result now equals 'done'
});

将此示例与您的代码进行比较:问题是您的map回调生成的第一个函数具有错误的签名,因为它应该只接受一个参数。

何时nextCallback(null, itemResult)被调用,nextCallback未定义。

于 2016-04-25T18:14:02.717 回答
0

根据文档,第一个函数的签名应该是function (callback)而不是function (arg1, callback)

您可以通过这种方式修补您的功能。

return function (lastItemResult, nextCallback) {
    if (!nextCallback) {
      nextCallback = lastItemResult;
      lastItemResult = 0;
    }

    // same execution for each item in the array
    var itemResult = (arrayItem+lastItemResult);
    // results carried along from each to the next
    nextCallback(null, itemResult);
};
于 2016-04-25T18:16:53.593 回答