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

时间:2012-09-24 17:10:21

标签: javascript meteor

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

(怎么样)可能吗?

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

4 个答案:

答案 0 :(得分:36)

使用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();
  }
});

请参阅this gist

答案 1 :(得分:26)

Meteor的最新版本提供了未记录的Meteor._wrapAsync函数,该函数将带有标准(err, res)回调的函数转换为同步函数,这意味着当前光纤会在回调返回之前产生,然后使用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

答案 2 :(得分:19)

Andrew Mao是对的。对于这种情况,流星现在有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: Proper use of Meteor.wrapAsync on server

答案 3 :(得分:5)

另一种选择是package,它实现了类似的目标。

meteor add meteorhacks:async

来自自述文件包:

<强> Async.wrap(功能)

包装异步函数并允许它在没有回调的情况下在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;
  }
});