Fibers in Meteor

This worked great in Meteor 1.2
Is this still the way to go for async calls?

Meteor.methods({
    myMeteorMethod: function() {
      // load Future
      Future = Npm.require('fibers/future');
      var myFuture = new Future();
      
      // call the function and store its result
      SomeAsynchronousFunction("foo", function (error,results){
        if(error){
          myFuture.throw(error);
        }else{
          myFuture.return(results);
        }
      });
  
      return myFuture.wait();
    }
  });

That will work, but it’s a lot of boilerplate for a standard async npm function. All you need to do is:

Meteor.methods({
  myMeteorMethod() {
    const syncSomeAsynchronousFunction = Meteor.wrapAsync(SomeAsynchronousFunction);
    try {
      return syncSomeAsynchronousFunction("foo");
    } catch (err) {
      throw new Meteor.Error('oops', 'something broke');
    }
  });
1 Like