Skip to content

Instantly share code, notes, and snippets.

@dkordik
Last active December 28, 2015 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkordik/7506482 to your computer and use it in GitHub Desktop.
Save dkordik/7506482 to your computer and use it in GitHub Desktop.
Better Meteor async. Man, futures can be confusing! This is how I clean things up a little bit in my actual methods.
var Future = Npm.require('fibers/future'); //npm install fibers
var async = function (callback) { //let's tuck away some of the nastyness in here
var future = new Future();
var returnFunc = function () {
future["return"].apply(future, arguments);
}
callback(returnFunc);
return future.wait();
}
//server
Meteor.methods({
someMethod: function () {
return async(function (done) {
someAsyncMethod(function (data) {
done(data)
});
});
}
})
//meanwhile, on the client...
Meteor.call("someMethod", function (data) {
console.log("data!", data)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment