Meteor:在Meteor.method中调用异步函数并返回结果 [英] Meteor: Calling an asynchronous function inside a Meteor.method and returning the result
本文介绍了Meteor:在Meteor.method中调用异步函数并返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Meteor方法中调用异步函数,然后将该函数的结果返回给Meteor.call。
I want to call an asynchronous function inside a Meteor method and then return the result from that function to Meteor.call.
(如何)可能?
Meteor.methods({
my_function: function(arg1, arg2) {
//Call other asynchronous function and return result or throw error
}
});
推荐答案
Andrew Mao是对的。对于这种情况,Meteor现在有 Meteor.wrapAsync()。
Andrew Mao is right. Meteor now has Meteor.wrapAsync() for this kind of situation.
这是通过条带进行收费的最简单方法,也可以传递回调函数:
Here's the simplest way to do a charge via stripe and also pass a callback function:
var stripe = StripeAPI("key");
Meteor.methods({
yourMethod: function(callArg) {
var charge = Meteor.wrapAsync(stripe.charges.create, stripe.charges);
charge({
amount: amount,
currency: "usd",
//I passed the stripe token in callArg
card: callArg.stripeToken,
}, function(err, charge) {
if (err && err.type === 'StripeCardError') {
// The card has been declined
throw new Meteor.Error("stripe-charge-error", err.message);
}
//Insert your 'on success' code here
});
}
});
我发现这篇文章真有帮助:
Meteor:在服务器上正确使用Meteor.wrapAsync
I found this post really helpful: Meteor: Proper use of Meteor.wrapAsync on server
这篇关于Meteor:在Meteor.method中调用异步函数并返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文