Converts a synchronous function to an asynchronous function
npm install to-async --save
toAsync(target, [cbFirst, [inNextFoop]])
target
target functioncbFirst
specifies callback position in arguments,true
first andfalse
lastinNextFoop
specifies when to run target function
var toAsync = require('to-async');
function syncFunc(toThrow) {
if (toThrow) {
throw new Error('Uppps')
}
}
function cb(err) {
if (err) {
console.log('Error', err);
return;
}
console.log('Success');
}
// Target function will be runned immediately
var asyncFunc = toAsync(syncFunc, false);
asyncFunc(true, cb);
asyncFunc(false, cb);
console.log('After calling to asyncFunc');
// Target function will be runned in next loop using setImmediate
var asyncNextLoopFunc = toAsync(syncFunc, false, true);
asyncNextLoopFunc(true, cb);
asyncNextLoopFunc(false, cb);
console.log('After calling to asyncNextLoopFunc');