Last active
December 28, 2015 13:19
-
-
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.
This file contains hidden or 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
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