Meteor.methods({
'wrapTest': function() {
var fs = Npm.require('fs');
getFileContents = function(callback) {
fs.readFile('/Users/anoop/delete/juhi.txt', 'utf8', function(err, res) {
if (err) console.log(err);
else callback && callback( null, res + 'modified' );
});
};
var getFileContentsSynchronously = Meteor.wrapAsync(getFileContents);
var result = getFileContentsSynchronously();
console.log(result);
return result;
}
});
Calling the above method logs { file contents + ‘modified’ } to console.
We are not passing any callback function when creating var getFileContentsSynchronously or when calling it
my confusion is how does the else block above know what function definition to use when calling
wrapAsync assumes the convention of the wrapped function signature being (optionalParameters, callback) with the callback signature being (error, result). In your example, your getFileContents function has no parameters, but does have a callback with an (error, result) signature (so it conforms to the assumption).
Normally, the required function would be wrapped more closely, which perhaps makes it clearer:
getFileContents = Meteor.wrapAsync(fs.readFile, fs); // assumed final callback(error, result)
try {
var result = getFileContentsSynchronously('/Users/anoop/delete/juhi.txt', 'utf8');
} catch (error) {
...
}