Files
sage/assets/build/util/promisify.js
2016-11-06 19:40:13 -08:00

22 lines
650 B
JavaScript

/**
* Node-style asynchronous function.
*
* @callback nodeAsyncCallback
* @param {string|null} err
* @param {*} data
*/
/**
* Promisify node-style asynchronous functions
*
* @param {nodeAsyncCallback} fn - Function with node-style callback
* @param {this} [scope] - Scope to which the function should be bound. Default: fn
* @returns {Promise} - An instance of Promise
*/
module.exports = (fn, scope) => function callback() {
const args = [].slice.call(arguments);
return new Promise((resolve, reject) => {
args.push((err, data) => (err === null ? resolve(data) : reject(err)));
return fn.apply(scope || fn, args);
});
};