Run Async
Utility method to run function either synchronously or asynchronously using the common this.async()
style. Also provides for promise returning functions.
This is useful for library author accepting sync or async functions as parameter. runAsync
will always run them as async method, and normalize the function handling.
Installation
npm install --save run-async
Usage
var runAsync = require('run-async');
var asyncFn = function (a) {
var done = this.async();
setTimeout(function () {
done(null, 'running: ' + a);
}, 10);
};
runAsync(asyncFn, function (err, answer) {
console.log(answer);
}, 'async');
var syncFn = function (a) {
return 'running: ' + a;
};
runAsync(syncFn, function (err, answer) {
console.log(answer);
}, 'sync');
var promiseFunc = function(a) {
return new Promise(function (resolve, reject) {
resolve('running: ' + a);
});
}
runAsync(promiseFunc, function (err, answer) {
console.log(answer);
}, 'promise');
Licence
Copyright (c) 2014 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.