【发布时间】:2014-08-04 14:15:24
【问题描述】:
具有两个调用但第二个调用的瀑布函数不等待第一个调用完全完成。第一个有一个 mongodb.find() 调用。 这是异步瀑布函数
app.get("/news", function(req, res) {
async.waterfall([
function (callback) {
var blogs = tendigiEngine.getAllBlogs(callback);
callback(null, blogs);
},
function (blogs, callback) {
var array = tendigiEngine.seperateBlogs(blogs, callback);
callback(null, array );
}
], function (err, result) {
// result now equals 'done'
console.log("done");
console.log(result);
});
});
下面是两个被调用的函数: getAllBlogs():
exports.getAllBlogs = function() {
Blog.find(function(err, theBlogs){
if(!err) {
return theBlogs;
}
else {
throw err;
}
});
}
单独的博客():
exports.seperateBlogs = function(blogs) {
if(blogs.length === 0 ) {
return 0;
}
else {
blogs.reverse();
var blog = blogs[0];
blogs.shift();
var finArray = [blog, blogs];
return finArray;
}
console.log("asdf");
}
在getAllBlogs() 返回theBlogs 之前不会调用seperateBlogs 很重要,但它会在返回值之前被调用。我使用 Async_Waterfall 来避免这个问题,但它不断重复出现,这意味着我用错了。我在这里做错了什么,我该如何解决?
谢谢!
【问题讨论】:
标签: javascript node.js mongodb asynchronous