My goal is to go through a loop asynchronously:
client.js
:
abc = function() {
for (var i = 0; i <= 49; i++) {
console.log(i);
Meteor.call('testBla', i)
}
}
server.js
testBla: function(i) {
function asyncCall() {
console.log('inside asyncCall', i)
return 'done';
}
var syncCall = Meteor.wrapAsync(asyncCall);
console.log('a');
var res = syncCall(i);
console.log('b')
return res;
}
Console:
a
inside asyncCall 0
Why does it stuck?
Functions you can pass to
Meteor.wrapAsync
must have a specific signature : their arguments must end with a callback given 2 arguments : error and result.Inside an async function body, you must invoke the callback with either an error in case the function fails, or the result if everything is OK.
You can only wrap functions that respect this signature and behavior, which is a standard inherited from Node.JS.
When you wrap async functions, don't forget to use a try/catch block if you want to handle the potential error.