39

I want to call an asynchronous function inside a Meteor method and then return the result from that function to Meteor.call.

(How) is that possible?

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

4 Answers 4

36

Use a Future to do so. Like this:

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

Update:

To use Future starting from Meteor 0.5.1, you have to run the following code in your Meteor.startup method:

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

  // use Future here
});

  Update 2:

To use Future starting from Meteor 0.6, you have to run the following code in your Meteor.startup method:

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

  // use Future here
});

and then use the return method instead of the ret method:

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

See this gist.

9
  • Thank you, Joscha. A future is exactly what I need. Sep 26, 2012 at 7:30
  • @KONG, I updated my answer, see above. I also forked the initial gist to include that information: gist.github.com/4130605
    – Joscha
    Dec 31, 2012 at 14:19
  • @Joscha, Thanks a lot for your solution! I managed to google it myself. Future is such a common name for package :)
    – KONG
    Dec 31, 2012 at 14:23
  • here's a full working demo of this: github.com/semateos/meteor-async-test
    – semateos
    Feb 27, 2013 at 15:32
  • As of 0.6.0 you have to use Future = Npm.require('fibers/future').
    – datacarl
    Apr 6, 2013 at 9:21
26

Recent versions of Meteor have provided the undocumented Meteor._wrapAsync function which turns a function with a standard (err, res) callback into a synchronous function, meaning that the current Fiber yields until the callback returns, and then uses Meteor.bindEnvironment to ensure that you retain the current Meteor environment variables (such as Meteor.userId()).

A simple use would be as the following:

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     
  }
});

You may also need to use function#bind to make sure that asyncFunc is called with the right context before wrapping it.

For more information see: https://www.eventedmind.com/tracks/feed-archive/meteor-meteor-wrapasync

1
19

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

        });
    }
});

I found this post really helpful: Meteor: Proper use of Meteor.wrapAsync on server

3
  • 2
    if you return a value in the callback to charge() will that be the return of charge()? Jan 20, 2015 at 10:29
  • 2
    Not clear where you're returning a value which is the point of the original question
    – gman
    Oct 25, 2015 at 8:27
  • 2
    This post is extremely helpful: themeteorchef.com/snippets/synchronous-methods
    – jetlej
    Apr 28, 2016 at 16:30
5

Another option is this package which achieves the similar goals.

meteor add meteorhacks:async

From the package README:

Async.wrap(function)

Wrap an asynchronous function and allow it to be run inside Meteor without callbacks.

//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;
  }
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.