异步瀑布获取 Typeerror,nextcallback 不是函数
async waterfall getting Typeerror, nextcallback is not a function
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
});
所以我是异步的新手,尝试了一个简单的例子但是得到了这个错误,这个方法有什么问题类型错误:nextCallback 不是一个函数
上面的代码有什么问题?
如果您看一下 async docs,您会注意到 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
未定义。
根据 documentation,第一个函数的签名应该是 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);
};
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
});
所以我是异步的新手,尝试了一个简单的例子但是得到了这个错误,这个方法有什么问题类型错误:nextCallback 不是一个函数
上面的代码有什么问题?
如果您看一下 async docs,您会注意到 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
未定义。
根据 documentation,第一个函数的签名应该是 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);
};