流星wrapAsync语法 [英] Meteor wrapAsync syntax
问题描述
如何使用流星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;
}
我想同步运行这两个Meteor.call
函数,但是我在console.log(getGroup);
上得到了undefined
,而shuold只是返回了一个对象.
I want to run these two Meteor.call
functions synchronosly but I get undefined
on console.log(getGroup);
which shuold just return an object.
推荐答案
Meteor.wrapAsync
是服务器端API,旨在包装需要回调作为最后一个参数的Node.js异步函数,以使其通过使用来同步显示. Future
s,光纤子库. (有关更多信息,请参见: https://www.discovermeteor.com/blog/wrapping-npm-packages/)
Meteor.wrapAsync
is a server-side API designed to wrap Node.js asynchronous functions requiring a callback as last argument, to make them appear synchronous through the use of Future
s, a Fibers sub-library. (more on this here : https://www.discovermeteor.com/blog/wrapping-npm-packages/)
不打算在客户端将异步Meteor.call
转换为同步调用,因为在浏览器上,远程方法调用"调用始终是异步的.
It is not intended to be used client-side to turn asynchronous Meteor.call
into a synchronous call because on the browser, Remote Method Invokation calls are ALWAYS asynchronous.
长话短说,您根本无法实现您要执行的操作,您必须使用回调并将第二个方法调用嵌套在第一个方法调用的成功回调中.
Long story short, you simply cannot achieve what you're trying to do, you have to use callbacks and nest your second method call inside the success callback of your first method call.
这篇关于流星wrapAsync语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!