我正在尝试使用Async.js触发一系列异步函数。下面是我的代码。仅执行前两个函数。该系列中的第三个和第四个函数不执行。我已经将函数思想简化为最基本的可能。但他们仍然不执行。有人可以告诉我我做错了什么吗?
async.series([
guessCollection.find( { user: user, imageFileName: imageFileName } ).count( function(err, number) {
count = number;
console.log(count);
}),
guessCollection.find( { user: user, imageFileName: imageFileName, correct: '1' } ).count( function(err, number) {
correct = number;
console.log(correct);
}),
function(){
console.log("this text never doesn't get logged");
},
function() {
console.log("neither does this text");
}
]);
编辑 --- 正如下面答案中所建议的,我制作了前两个正确的功能。然而现在只执行该系列中的第一个函数。函数 2-4 不会被调用。我认为这段代码中肯定还有其他问题。
async.series([
function(){
guessCollection.find( { user: user, imageFileName: imageFileName } ).count( function(err, number) {
count = number;
console.log(count);
})
},
function(){
guessCollection.find( { user: user, imageFileName: imageFileName, correct: '1' } ).count( function(err, number) {
correct = number;
console.log(correct);
})
},
function(){
console.log("this text never doesn't get logged");
},
function() {
console.log("neither does this text");
}
]);
最佳答案
看一下这段代码,它只输出了1 2 3,因为第3个函数没有调用回调函数,所以系列到这里就停止了。 http://jsfiddle.net/oceog/9PgTS/
async.series([
function (c) {
console.log(1);
c(null);
},
function (c) {
console.log(2);
c(null);
},
function (c) {
console.log(3);
// c(null);
},
function (c) {
console.log(4);
c(null);
},
]);
关于javascript - 并非 Async.js 系列中的每个函数都会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13003178/