say I have the following function which has a synchronous filesystem call enclosed:
var foo = function(){
var numbytes = fs.readSync(...);
return numbytes;
}
no problems there.
but if I use an async function with a callback like so:
var baz = function(){
var numbytes = fs.read(...,function(){
// return numbytes; (wrong)
});
//return numbytes; (wrong)
};
how do I properly return a value tied to numbytes from the baz function?
In the Meteor Node.js framework, this might be one answer to this problem:
Meteor.wrapAsync(func, [context]) Anywhere
Wrap a function that takes a callback function as its final parameter. On the server, the wrapped function can be used either synchronously (without passing a callback) or asynchronously (when a callback is passed). On the client, a callback is always required; errors will be logged if there is no callback. If a callback is provided, the environment captured when the original function was called will be restored in the callback.
Arguments
func Function
A function that takes a callback as its final parameter
context Object
Optional this object against which the original function will be invoked