2016-02-12 11:40:21 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*eslint-disable prefer-arrow-callback*/
|
|
|
|
/**
|
|
|
|
* Function to convert a callback function into a promise
|
|
|
|
*
|
2016-02-12 12:50:59 -05:00
|
|
|
* @private
|
2016-02-12 11:40:21 -05:00
|
|
|
* @see http://eddmann.com/posts/promisifying-error-first-asynchronous-callbacks-in-javascript/
|
|
|
|
* @example promisify(fs.readFile)('hello.txt', 'utf8')
|
|
|
|
* .then(console.log)
|
|
|
|
* .catch(console.error)
|
|
|
|
* @param {Function} fn - the callback function to convert
|
|
|
|
* @return {Promise} - the new promise
|
|
|
|
*/
|
|
|
|
function promisify(fn) {
|
|
|
|
return function () {
|
|
|
|
let args = [].slice.call(arguments);
|
2016-02-12 12:50:59 -05:00
|
|
|
return new Promise(function (resolve, reject) {
|
2016-02-12 11:40:21 -05:00
|
|
|
fn.apply(undefined, args.concat((error, value) => {
|
|
|
|
return error ? reject(error) : resolve(value);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = promisify;
|
|
|
|
/*eslint-enable prefer-arrow-callback*/
|