async-compat
Convert synchronous, callback, and promise-returning functions to a callback interface.
Install
npm install async-compat
Usage
asyncFunction(fn, hasCallback, ...args, callback) invokes fn and calls the final callback with (err, result). Set hasCallback to true when fn expects a callback.
var compat = require('async-compat');
function syncFn(value) {
return value * 2;
}
compat.asyncFunction(syncFn, false, 2, function (err, result) {
if (err) throw err;
console.log(result); // 4
});
function callbackFn(value, callback) {
callback(null, value * 2);
}
compat.asyncFunction(callbackFn, true, 2, function (err, result) {
if (err) throw err;
console.log(result); // 4
});
function promiseFn(value) {
return Promise.resolve(value * 2);
}
compat.asyncFunction(promiseFn, false, 2, function (err, result) {
if (err) throw err;
console.log(result); // 4
});
API
asyncFunction(fn, hasCallback, ...args, callback)normalizes a synchronous, callback-based, or promise-returning function. ReturnedErrorvalues and rejected promises reach the callback aserr.asyncValue(value, callback)sends anError, resolved promise value, or ordinary value to the callback.defaultValue(result, value)returnsvalueonly whenresultisundefined; otherwise it returnsresult.