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
Meteor.wrapAsync
works on the client for the sake of isomorphic code. This is so you can make code that you can share on the client and server without Meteor crashing or complaining.
It is not possible to have synchronous code on the client. At least without using ES6 features which are not available on all browsers.
As in the comment by saeimeunt Meteor.wrapAsync will require a callback on the client.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With