2011-11-01 44 views
1

我试图用猫鼬和下划线在一起,做这样的事情:下划线和猫鼬:使用文档作为外部函数的返回值

var person_ids = [1, 2, 3]; 

var persons = _(person_ids).map(function(id) { 
    Person.findById(id, function(person) { // Non-blocking 
     // How do I use 'person' as the outer function's return value? 
    }); 
}); 

有没有办法做到这一点?我意识到我可能会试图在设计为异步使用的库上强制同步范例。

即可获得价值
+0

没不知道'findById'是异步的。看起来你被困在一个扭曲的迷宫当中。尽管尝试'Person.find'方法。 –

回答

0

唯一的办法就是通过回调:

在检查我的答案是:在代码 How to retrieve value of a variable in class

重要的地方是:

Place.getActualId(function(id){ console.log(id); }); 

getActualId: function(callback){ 
     this.find({where: {actual: 1}}).on('success', function(placeTmp){ 
     callback(placeTmp['id']); 
}) 
+0

是的,这是我对Mongoose图书馆工作原理的理解。但令人失望的是,它与Underscore提供的功能完全不兼容。 –