cro*_*ryo 17 methods synchronization call synchronous meteor
我需要一种方法使流星调用同步,以便在运行调用时代码等待结果完成,以便它可以继续到客户端上的下一行代码.
例如:
clientFunction = function(){
Meteor.call('serverFunction', function(err,result){})//<--so when this gets a result and
//is stored in a session variable
var doSomeThing = Session.get('whatever') <-- so that your able to use it here
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试做一个while循环来防止任何事情发生,直到返回一个值,但似乎它在clientFunction之后运行,从而将其抛给它的死亡
任何帮助都会被贬低
Chr*_*itz 11
这是一个非常常见的问题,被问到各种形状和形式.大多数人都没有意识到他们何时进行异步调用.但是,解决方案始终是相同的:将服务器上的方法代码包装成光纤或使用未来.
我认为最好的做法是使用Meteor._wrapAsync
所描述的当前可用功能,例如:
Meteor:在Meteor.method中调用异步函数并返回结果
其他一些选项如下所述:https: //gist.github.com/possibilities/3443021
更新:现在调用该方法Meteor.wrapAsync
.