[SOLVED] I have no idea how Meteor.wrapAsync works


#1

Hello everyone,

I’ve written a username generator for my application this morning. Here is it’s source code. As you can see, I just call cat/grep on my OSs dictionaries.

It works. Perfectly. But I have no idea why. The question come when you see pickAdjective and pickNoun functions. They make an exec call and then… What? They don’t return anything and if you see the next lines, I never define any callbacks. There is not a single line of code that says “hey, just return stdout” or whatever. But the code works and returns the proper values at all times.

I’m not complaining of course, it just blows my mind and I feel uncomfortable not understanding the process


#2

Never used wrapAsync, but it seems useless here. Maybe you can try to remove it and see what happens:

var pickAdjective = function(callback){
  exec("cat /usr/share/dict/words | grep ...", callback);
};
var pickNoun = function(callback){
  exec("cat /usr/share/dict/words | grep ...", callback);
};

#3

This will not work, because exec runs asyncronously and I need my method to return their result.

It’s as if in client-side JS you’d do something like

$.ajax('/REST/nouns/pick/1', callback);
$.ajax('/REST/adjectives/pick/1', callback);

#4

Didnt look at ur code but this video explains it a bit. IIRC .wrapAsync has changed a bit in the last few releases so check the docs.

https://d2ua9xepcqu1mi.cloudfront.net/content/meteor-meteor-wrapasync-Ww3rQrHJo8FLgK7FF/videos/video-1.mp4?Signature=S6N-JfYJ-9daK1kYGc5unw2dNlNg9jFr3ZzTTQjwuWSXGr-5axXzufUGIM76UM8E8AMJhc18Dl6n3Zg6V6uGrTFuoutdVMxfrEyjgr2MikxoCpR3oP1g50csz5OPjokiEjO7S9vw-TxNgenYbJC3cS8EEHKT9xz4kdsHbF3oBFwTTU6DspXIQDiwE~~1dvbwSA2ADef7C57p-P1m~hvIu4ZY46rKCDjz~LOU5Lie58tpWvwnlvR392pONvGCfjpXsH6UapQdq8YsTGbpuWniO~5pOXkccGtcJNEbw~uvK1raMx-JmMe1BA1oyLVUNIJLmQ0tH1LTu7mrPG6KatEZLQ__&Key-Pair-Id=APKAJHBOCGGNI5ZCTVGQ&Expires=1431890443


#5

Indeed that helped a lot. What wrapAsync does seems to do is that it just returns the second parameter of the passed function by convention.

Of course that means that if the callback has a signature different than one of (err, result) wrapAsync will probably return unpredictable results.