等着流星.客户端的呼叫结果

Wait for Meteor.call result on Client

本文关键字:呼叫 结果 客户端 流星      更新时间:2023-09-26

我是JavaScript新手。我不明白如何等待流星的结果。调用方法。这是我的代码

     //client/main.js
     //Added the callback
        Template.hello.events({
  'click button'(event, instance) {
    // increment the counter when button is clicked
    instance.counter.set(instance.counter.get() + 1);
    var res = Meteor.call("callMeLater","sanj",function (err,res) {
      if (err) {
        console.log(err);
      } else {
        console.log("this is the result main ", res);
      }
    });
    console.log("this is the result ", res);
  }
        //server/main.js
        Meteor.methods({
        callMeLater :function (name) {
            var callMeLaterSync =Meteor.wrapAsync(callMeLaterAsync);
            var result = callMeLaterSync(name);
            console.log("this is the test", result);
            return result;
        }
    });

    var callMeLaterAsync = function (name,cb) {
        setTimeout(function () {
            cb && cb (null ,"hey there, "+name);
        },2000);
    };

在控制台上,我得到

this is the result  undefined
this is the result main  hey there, sanj

我如何等待流星的结果。通过在客户端阻塞执行来调用。请帮助

谢谢

只需将代码放入回调方法中。

   Meteor.call('callMeLater',"sanj", function(err, res){
    if (err) {
      console.log(err);
    } else {
      console.log("this is the result ", res);
    }
  });