我在这个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(异步代码以同步样式编写)。在客户端上,您可以使用无功变量。让我写一个答案显示它。 –