Skip to content

Instantly share code, notes, and snippets.

@rmm5t
Created August 15, 2017 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmm5t/f2a95c2ef6485132ffa7789863bfa44d to your computer and use it in GitHub Desktop.
Save rmm5t/f2a95c2ef6485132ffa7789863bfa44d to your computer and use it in GitHub Desktop.
Node.js "promisify" implementations
const promisify = (fn) => {
return function () {
const args = Array.from(arguments);
return new Promise(function (resolve, reject) {
fn.apply(undefined, args.concat([function (error, data) {
if (error) return reject(error);
if (data === undefined) return resolve();
return resolve(data);
}]));
});
};
};
const promisify = (fn) => {
return (...args) => {
return new Promise((resolve, reject) => {
fn(...args, (error, data) => {
if (error) return reject(error);
if (data === undefined) return resolve();
return resolve(data);
});
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment