Recording / Converting Synchronous Meteor Functions

This bothered me for a while, so I thought I would just do a quick QA:

If you have a regular nodeJS module or something else, and it has an asynchronous function on the server side. How to make it synchronous. For example, how to convert the asynchronous function nodejs fs.stat to synchronous.

for example, I have

server side js

 Meteor.methods({ getStat:function() { fs.stat('/tmp/hello', function (err, result) { if (err) throw err; console.log(result) }); } }); 

If I call this from the client, I return undefined as the result, because the result is in the callback.

+5
asynchronous meteor node-fibers sync
source share
1 answer

There is a function (undocumented) called Meteor.wrapAsync .

Just wrap the function up

 Meteor.methods({ getStat:function() { var getStat = Meteor._wrapAsync(fs.stat); return getStat('/tmp/hello'); } }); 

Now you will get the result of this in the result your Meteor.call . You can convert any asynchronous function with a callback, where the first parameter is an error and the second is the result.

+7
source share

All Articles