Skip to content

Instantly share code, notes, and snippets.

@gnitnuj
Created August 27, 2020 13:40
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 gnitnuj/a13188cf82c74ea5ced97452799668f0 to your computer and use it in GitHub Desktop.
Save gnitnuj/a13188cf82c74ea5ced97452799668f0 to your computer and use it in GitHub Desktop.
Polyfill for util.promisify. Converts NodeJS Style callbacks into promises
/**
* 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);
});
});
};
};
},
};
{
"name": "promisify",
"version": "0.1.0"
}
// 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