Meteor wrapAsync语法

jt1*_*123 5 synchronous meteor

我如何使用Meteor wrapAsync

以下是我想要做的事情

if (tempTreatment.groupId === undefined) {
      // create new group
      Meteor.wrapAsync(Meteor.call('createTreatmentGroup', salon, tempTreatment.groupName, tempTreatment.groupName));

      // get group id
      var getGroup = Meteor.wrapAsync(Meteor.call('getTreatmentGroup', salon, tempTreatment.groupName));

      console.log(getGroup);
      tempTreatment.groupId = getGroup._id;
}
Run Code Online (Sandbox Code Playgroud)

我想Meteor.call同步运行这两个函数,但我得到undefinedconsole.log(getGroup);哪个shuold只返回一个对象.

sai*_*unt 7

Meteor.wrapAsync是一个服务器端API,旨在包装需要回调作为最后一个参数的Node.js异步函数,以通过使用Futures(Fibers子库)使它们看起来是同步的.(更多相关内容:https://www.discovermeteor.com/blog/wrapping-npm-packages/)

它不打算用于客户端将异步Meteor.call转换为同步调用,因为在浏览器上,Remote Method Invokation调用总是异步的.

长话短说,你根本无法实现你想要做的事情,你必须使用回调并在第一个方法调用的成功回调中嵌套你的第二个方法调用.

  • 我没有注意到这一点.我认为他们制作了一个客户端版本的"Meteor.wrapAsync",以便使用它的代码可以放在共享文件夹中而不会触发错误,但实际上这是供服务器使用的.如果没有提供回调,`wrapAsync`的客户端版本只是定义了一个标准的`logErr`回调来代替,如果存在则只记录错误:https://github.com/meteor/meteor/blob/ 9608e6205019b69a302cde62e21fcae1c7d22e3d /包/流星/ helpers.js#L108 (3认同)