Created
August 15, 2017 01:32
-
-
Save rmm5t/f2a95c2ef6485132ffa7789863bfa44d to your computer and use it in GitHub Desktop.
Node.js "promisify" implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}])); | |
}); | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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