2014-02-19 32 views
0

我有一個類似的代碼:使用纖維/未來異步回調客戶端

Template.mytemplate.pippo = function() { 
    var returnValue; 
    asyncFunc(function (dataReturned) { 
     returnValue = dataReturned; 
    }); 

    return returnValue; 
} 

我嘗試在客戶端

var Future = Npm.require('fibers/future'); 

加載未來,但沒有工作:(

如何等待asyncFunc返回callBack finish返回模板值returnValue

謝謝!

+0

在客戶端,你將不得不使用異步代碼。真的沒有辦法超過這個。也許就代碼給出你想要做什麼,也許你可以使用Session變量來處理返回反應 – Akshat

+0

節點光纖是用C++編寫的,並且不能在瀏覽器中使用。您可能需要查看ES6生成器和yield關鍵字,但是您將無法在大多數瀏覽器中使用它們。 – sbking

+0

瞭解...我會使用的代碼是上面的(在主帖子中)。你是否建議在asyncFunc的回調中插入一個Session.Set,並製作了另一個模板助手來檢查Session ......是不是正確? – elbowz

回答

0

有一點,你應該叫asyncFunc的問題,但一旦你明白這一點,你的代碼應該是這個樣子:

asyncFunc(function (dataReturned) { 
    Session.set('returnValue', dataReturned); 
}); 

... 

Session.setDefault('returnValue', "loading.."); // or some other default that is safe 
Template.mytemplate.pippo = function() { 
    return Session.get('returnValue'); 
}