Skip to content

Instantly share code, notes, and snippets.

@SachaG
Last active November 17, 2016 09:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SachaG/5828399 to your computer and use it in GitHub Desktop.
Save SachaG/5828399 to your computer and use it in GitHub Desktop.
Call an asynchronous function and use its returned value from within a Meteor method using Future.
if(Meteor.isServer){
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();
}
});
}
@thinklinux
Copy link

That "return myFuture.wait();" saved me from a lot of wasted time : ) Thanks. In many examples it's just "myFuture.wait()" and that way you can't return any value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment