Meteor 如何使流星方法同步?

Meteor 如何使流星方法同步?,meteor,methods,synchronization,call,synchronous,Meteor,Methods,Synchronization,Call,Synchronous,我需要一种使meteor调用同步的方法,以便在运行调用时,代码等待结果完成,以便可以继续到客户端上的下一行代码 例如: clientFunction = function(){ Meteor.call('serverFunction', function(err,result){})//<--so when this gets a result and //is sto

我需要一种使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
}
clientFunction=function(){
Meteor.call('serverFunction',function(err,result){}/我按照教程进行了如下操作

这是meteor服务器端方法

productIdResult:function(searchstring)
{
   {
      var skimlinks_query = Async.wrap(skimlinks.query);
      var title_val="title:\"electric bicycle\"  AND merchantCategory:*bikes*";                     
      var result = skimlinks_query({
                        searchFor: searchstring,
                        start:start,
                        rows:rows,
                        fq: "country:US"
                    });
      return result;
}
我从客户那里这样称呼它

Meteor.call('productIdResult',
            searchstring,
            function(error,resul)
            {
                arr[0]=resul.skimlinksProductAPI.products[0].title;
                $( "#op1").autocomplete({source:arr});

            }
);

请注意,它不是同步的,但您会在回调中得到返回值。这是一个非常常见的问题,以各种形式被问到。大多数人在进行异步调用时都没有意识到。但是,解决方案总是一样的:将服务器上的方法代码封装到光纤中或使用未来的方法

我认为,最佳做法是使用当前可用的Meteor.\u wrapAsync
功能,如下所述,例如:

此处介绍了一些其他选项:



更新:该方法现在称为
Meteor.wrapAsync

将方法完成后要运行的代码放入方法回调中。这是任何异步javascript的标准

clientFunction = function(){
  Meteor.call('serverFunction', function(err, result){
    if (err) {
      alert(err);
    } else {
      Session.set('whatever', result.whatever);
    }
  });
};
一旦方法调用返回,就设置会话变量的值。现在使用Meteor的反应性来使用该变量:

Template.hello.helpers({
  myWhatever: function () {
    var whatever = Session.get('whatever');

    if (whatever) return whatever;

    return 'Loading whatever...';
  }
});

你为什么不在回调中设置/doSomething呢?我会这样做,直到客户机代码运行完成后,它才会设置值,如果你解释一下你想做什么,这可能会有帮助。使用当前代码,你可以将
var doSomething
代码移到回调中。基于什么你有,没有理由不这样做。阻止客户端不是一个好主意,异步回调就是为了防止这种情况。我有7个集合,带有指定的分离类,用于在单个级别处理类,例如this.add=函数(名称、编号、状态、类型、品牌、代理){返回项目。插入({name:name,number:number,status:status,visibility:true,dateCreated:new Date(),dateModified:new Date(),type:type,brand:brand,agency:agency,spot:[],archive:false})};但我无法在函数中调用以重新运行该值我确实尝试过,但控制台不断地抛出“Object has no method _wrapAsync”对象,这意味着您可能正在调用客户端?我刚刚确认,在meteor的最后两个版本中,该函数仍然存在:在服务器上:
console.log(Meteor.\u wrapAsync)
打印
[功能]
,即它存在。如果您正在等待
Meteor.call的值,以便在下一行代码中执行某些操作,
Meteor.call
是异步的,这意味着根据定义,
Meteor.call之后的下一行代码将不会等待其完成。这就是任何异步调用的方式nous调用有效。这就是异步调用具有回调的原因,它允许您仅在返回结果后执行代码。如果您需要等待返回值,则需要输入“您的下一行代码”为了完整性,Meteor提供了一种替代方法,可以将依赖于返回值的代码放入回调中。相反,让依赖于返回值的代码使用反应变量(如
会话
)然后在回调中将返回值存储在反应变量中。我完全理解异步调用,我说的是您的解决方案只适用于模板,因为反应属性而不是通用解决方案,
\u wrapAsync
看起来是一种更好的方法…将取决于远程调用结果的代码放入callba中ck是一个通用的解决方案,是编写javascript异步代码的标准方式。Meteor利用服务器端来避免这种情况。这个问题的上下文是在客户端运行的代码(我们可以这样说,因为原始问题包括使用仅限客户端的
会话
wrapAsync
(使用光纤)是一个很好的服务器端代码解决方案。