我在這個Meteor.js中很新。在我的Template.myTemplate.onCreated()
裏面,我使用Meteor.call()
調用兩個服務器方法。現在問題是在Meteor.call()中執行回調函數中的代碼,在執行onCreated函數內的剩餘代碼後執行。同樣的事情發生在幫手和事件。爲什麼Meteor js不能在Meteor.call上串行執行()
是不是?或者我做錯了什麼?
如果它是正確的,那麼有什麼辦法可以做代碼將連續執行?
實例讓你更好地理解:
客戶端/ myTemplate.js
Template.myTemplate.created = function(){
console.log('created start');
Meteor.call('myTestMethod1', function(err, res){
if(res){
console.log(res);
}
});
Meteor.call('myTestMethod2', function(err, res){
if(res){
console.log(res);
}
});
console.log('created end');
}
服務器/ method.js
Meteor.methods({
myTestMethod1 : function(){
return "myTestMethod1";
},
myTestMethod2 : function(){
return "myTestMethod2";
}
});
控制檯:
created start
created end
myTestMethod2
myTestMethod1
有什麼想法...
我建議你搜索單詞「競賽條件」。它可以幫助你發現你的代碼正在做什麼以及爲什麼。 –
@Kyll ** race condition **是'兩個線程/控件試圖同時訪問同一個數據',我知道它......但是你的想法和流星一樣是爲了防止**競爭條件** ??? – iamhimadri
好吧,當你有API調用時,競爭條件是一個永久的問題。 Meteor以多種方式解決它...在服務器上,使用Fibres(異步代碼以同步樣式編寫)。在客戶端上,您可以使用無功變量。讓我寫一個答案顯示它。 –