<分区>
我正在使用 Async调用一个函数,在该函数中它异步调用 db 并获取一些值,然后将该值返回给 async.series 的回调。但是我怎样才能将值作为外部函数的 return
值传递呢?
更多信息请引用下面代码中的注释。
getUserObjFromDb = function(username, cb) {
var user = {};
async.series([
function(callback){
var userObj = callDb(username);
callback( null, userObj );
}
],
function(err, results){
user = results[0];
cb(user);
});
}
var theUser = getUserObjFromDb(username, function(user){
return user;
}); // theUser is undefined because the outer function getUserObjFromDb did not return anything even though its callback returned, it was only the callback of the async.series which returned.
如何获取 user
传递给 theUser
的值?
我可能做的所有这些都是完全错误的,并且可能有更简单的方法,所以任何帮助将不胜感激。 谢谢。