Created
August 27, 2020 13:40
-
-
Save gnitnuj/a13188cf82c74ea5ced97452799668f0 to your computer and use it in GitHub Desktop.
Polyfill for util.promisify. Converts NodeJS Style callbacks into promises
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
/** | |
* Promisify | |
* Polyfill for util.promisify. Converts NodeJS Style callbacks into promises | |
*/ | |
const util = require('util'); | |
module.exports = { | |
polyfill() { | |
// turn node style callbacks into promises | |
util.promisify = | |
util.promisify || | |
function(fn) { | |
return function(...nodeArgs) { | |
return new Promise((resolve, reject) => { | |
fn(...nodeArgs, (err, ...args) => { | |
if (err) { | |
reject(err); | |
return; | |
} | |
if (!args || args.length === 0) { | |
return resolve(); | |
} | |
if (args.length === 1) { | |
return resolve(args[0]); | |
} | |
resolve(args); | |
}); | |
}); | |
}; | |
}; | |
}, | |
}; |
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
{ | |
"name": "promisify", | |
"version": "0.1.0" | |
} |
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
// this file exists just to name the gist, and because gist ordering is asciibetical, it's name starts with a capital letter. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment