Skip to content

Instantly share code, notes, and snippets.

@santiq santiq/Promisify.js
Last active Jun 23, 2017

Embed
What would you like to do?
Promisify a function with callback
// Needs ... notation to be available
const promisify = (fn) => {
return (...args) => {
return new Promise((resolve, reject)=>{
fn(...args, function(err, res){
if(err){
return reject(err);
}
return resolve(res);
})
})
}
}
// Polified version
"use strict";
var promisify = function promisify(fn) {
return function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return new Promise(function (resolve, reject) {
fn.apply(undefined, args.concat([function (err, res) {
if (err) {
return reject(err);
}
return resolve(res);
}]));
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.