user3555373
user3555373

Reputation: 149

Meteor & Promises?

I'm having a hard time understanding the docs for the Promise package in meteor, does Meteor support promises out of the box on the client side? I havent been able to find an example.

Upvotes: 1

Views: 207

Answers (3)

jbergs
jbergs

Reputation: 1

I've used FutureJS:

Future = Npm.require('fibers/future');
  var myFuture = new Future();

  SomeAsynchronousFunction("foo", function(err, res) {
    if (err) {
      myFuture.throw(err);
    } else {
      myFuture.return(res);
    }
  });

  return myFuture.wait();

So if you're making a GET request, replace SomeAsynchronousFunction()... with:

Meteor.http.call('GET', 'someUrl', function(err, resp) {
  if (err) {
    myFuture.return(err);
  } else {
    myFuture.return(resp);
  }
});

Upvotes: 0

Adam Moisa
Adam Moisa

Reputation: 1383

Sort of. Meteor methods has an async callback built into it:

Meteor.call('myMethod', foo1, function (err, res) {
  //this code waits for err or res
})

Upvotes: 0

Jesper We
Jesper We

Reputation: 6087

In the current Meteor (1.3) you do not need the Promise package. Include the ecmascript package instead, this will give you ES6 standard promises and also "await" support, which is much easier to comprehend than just promises, and makes the code more readable.

See for example https://forums.meteor.com/t/start-using-async-await-instead-of-promises-and-callbacks/17037 and https://www.twilio.com/blog/2015/10/asyncawait-the-hero-javascript-deserved.html

Upvotes: 2

Related Questions