Skip to content

Instantly share code, notes, and snippets.

@sonufrienko
Created January 19, 2019 10:19
Show Gist options
  • Save sonufrienko/44b4947caa579ad739de1547653efdc5 to your computer and use it in GitHub Desktop.
Save sonufrienko/44b4947caa579ad739de1547653efdc5 to your computer and use it in GitHub Desktop.
Custom JavaScript Promisify function
const promisify = f => (...args) =>
new Promise((resolve, reject) => {
f(...args, (err, res) => {
if(err) {
return reject(err);
}
return resolve(res);
});
});
@sonufrienko
Copy link
Author

Variant 1 - use Node.js util

const util = require('util');
const funcAsync = util.promisify(funcCallback);

Variant 2 - use custom function

const funcAsync = promisify(funcCallback);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment