2011-11-01 42 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提供的功能完全不兼容。 –