【发布时间】:2013-02-11 23:02:08
【问题描述】:
我正在使用parallel function in Async.js,由于某种原因,最终的回调没有被执行,我没有看到任何地方发生错误。
我正在动态创建一个函数数组,这些函数被传递给并行调用:
// 'theFiles' is an array of files I'm working with in a code-generator style type of scenario
var callItems = [];
theFiles.forEach(function(currentFile) {
var genFileFunc = generateFileFunc(destDir + "/" + currentFile, packageName, appName);
callItems.push(genFileFunc(function(err, results) {
if(err) {
console.error("*** ERROR ***" + err);
} else {
console.log("Done: " + results);
}
}));
});
async.parallel(callItems, function(err, results) {
console.log(err);
console.log(results);
if(err) {
console.error("**** ERROR ****");
} else {
console.log("***** ALL ITEMS HAVE BEEN CALLED WITHOUT ERROR ****");
}
});
然后在一个外部函数中(在上面执行 forEach 的函数之外)我有 generateFileFunc() 函数。
// Function that returns a function that works with a file (modifies it/etc).
function generateFileFunc(file, packageName, appName) {
return function(callback) {
generateFile(file, packageName, appName, callback);
}
}
我查看了this SO post,它帮助我到达了我所在的位置。但是最终的回调没有被执行。并行调用中的所有项目都在执行。在最底部的 gnerateFile (function) 内部,我调用了回调,所以这是金色的。
有人知道为什么这可能无法正常执行吗?
最终结果是并行处理每个函数调用,然后在我完成时收到通知,以便我可以继续执行其他一些指令。
谢谢!
【问题讨论】:
标签: node.js async.js node-async