In my Meteor client code, I'm trying to use a third party API that only has asynchronous calls. How can I use Meteor.wrapAsync on the client to make calls to this API in a synchronous style? The docs seem to indicate that this is possible: http://docs.meteor.com/#/full/meteor_wrapasync
Here is some sample code I would like to call in a synchronous style:
var func1 = function(callback) {
Meteor.setTimeout(function() {
console.log('func1 executing');
callback(null, {success: true});
}, 2000);
};
var func2 = function(callback) {
Meteor.setTimeout(function() {
console.log('func2 executing');
callback(null, {success: true});
}, 1000);
};
var wrapped1 = Meteor.wrapAsync(func1);
var wrapped2 = Meteor.wrapAsync(func2);
Template.test.rendered = function() {
wrapped1();
console.log('After wrapped1()');
wrapped2();
console.log('After wrapped2()');
};
Currently, this produces this output:
After wrapped1()
After wrapped2()
func2 executing
func1 executing
I'd like it to produce:
func1 executing
After wrapped1()
func2 executing
After wrapped2()
I've put this code into a MeteorPad here: http://meteorpad.com/pad/fLn9DXHf7XAACd9gq/Leaderboard