Last active
November 17, 2016 09:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.