Meteor:在 Meteor.method 中调用异步函数并返回结果



我想在 Meteor 方法中调用一个异步函数,然后将该函数的结果返回给 Meteor.call。

(如何)这可能吗?

Meteor.methods({
  my_function: function(arg1, arg2) {
    //Call other asynchronous function and return result or throw error
  }
});

使用 a Future 来执行此操作。喜欢这个:

Meteor.methods({
  my_function: function(arg1, arg2) {
    // Set up a future
    var fut = new Future();
    // This should work for any async method
    setTimeout(function() {
      // Return the results
      fut.ret(message + " (delayed for 3 seconds)");
    }, 3 * 1000);
    // Wait for async to finish before returning
    // the result
    return fut.wait();
  }
});

更新

若要从 Meteor 0.5.1 开始使用 Future,必须在 Meteor.startup 方法中运行以下代码:

Meteor.startup(function () {
  var require = __meteor_bootstrap__.require
  Future = require('fibers/future');
  // use Future here
});

 更新 2

若要从 Meteor 0.6 开始使用 Future,必须在 Meteor.startup 方法中运行以下代码:

Meteor.startup(function () {
  Future = Npm.require('fibers/future');
  // use Future here
});

然后使用 return 方法而不是 ret 方法:

Meteor.methods({
  my_function: function(arg1, arg2) {
    // Set up a future
    var fut = new Future();
    // This should work for any async method
    setTimeout(function() {
      // Return the results
      fut['return'](message + " (delayed for 3 seconds)");
    }, 3 * 1000);
    // Wait for async to finish before returning
    // the result
    return fut.wait();
  }
});

请参阅此要点。

最新版本的 Meteor

提供了未记录的 Meteor._wrapAsync 函数,该函数将具有标准(err, res)回调的函数转换为同步函数,这意味着当前的 Fiber 产生直到回调返回,然后使用 Meteor.bindEnvironment 来确保您保留当前的 Meteor 环境变量(例如 Meteor.userId()) )。

一个简单的用法如下:

asyncFunc = function(arg1, arg2, callback) {
  // callback has the form function (err, res) {}
};
Meteor.methods({
  "callFunc": function() {
     syncFunc = Meteor._wrapAsync(asyncFunc);
     res = syncFunc("foo", "bar"); // Errors will be thrown     
  }
});

您可能还需要使用 function#bind 来确保在包装之前使用正确的上下文调用asyncFunc

有关详细信息,请参阅:https://www.eventedmind.com/tracks/feed-archive/meteor-meteor-wrapasync

Andrew Mao是对的。Meteor 现在有 Meteor.wrapAsync() 来应对这种情况。

以下是通过条带进行收费并传递回调函数的最简单方法:

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

另一种选择是实现类似目标的软件包。

meteor add meteorhacks:async

从包自述文件:

Async.wrap(function)

包装一个异步函数,并允许它在 Meteor 中运行而无需回调。

//declare a simple async function
function delayedMessge(delay, message, callback) {
  setTimeout(function() {
    callback(null, message);
  }, delay);
}
//wrapping
var wrappedDelayedMessage = Async.wrap(delayedMessge);
//usage
Meteor.methods({
  'delayedEcho': function(message) {
    var response = wrappedDelayedMessage(500, message);
    return response;
  }
});

最新更新