How to use Meteor.wrapAsync on the client?

In my Meteor client code, I am trying to use a third-party API that only has asynchronous calls. How can I use Meteor.wrapAsync on the client to call this API in a synchronous style? The docs show that this is possible: http://docs.meteor.com/#/full/meteor_wrapasync

Here is a sample code that 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()'); }; 

This currently produces this conclusion:

 After wrapped1() After wrapped2() func2 executing func1 executing 

I would like it to produce:

 func1 executing After wrapped1() func2 executing After wrapped2() 

I put this code in MeteorPad here: http://meteorpad.com/pad/fLn9DXHf7XAACd9gq/Leaderboard

+6
source share
1 answer

Meteor.wrapAsync runs on the client for isomorphic code. This means that you can make code that you can use on the client and server without Meteor crashes or complaints.

It is not possible to have synchronous code on the client. At least without using ES6 features that are not available in all browsers.

As in the saeimeunt comment, Meteor.wrapAsync will require a callback on the client.

+2
source

Source: https://habr.com/ru/post/984756/


All Articles