mongoosejs异步代码。
userSchema.static('alreadyExists',function(name){
var isPresent;
this.count({alias : name },function(err,count){
isPresent = !!count
});
console.log('Value of flag '+isPresent);
return isPresent;
});
我知道 isPresent 在 this.count 异步函数调用回调之前被返回,所以它的值是 undefined。但是我该如何等待回调改变 isPresent 的值,然后安全地返回?
(function(){ asynccalls() asynccall() })(); 对异步流程有什么影响?如果 var foo = asynccall() 或 (function(){})() 会发生什么?以上两个会让返回等待吗? process.nextTick() 能帮助吗?
我知道有很多类似这样的问题,但没有一个解释了在异步完成之前返回的问题。