Skip to content

Instantly share code, notes, and snippets.

@simon-tiger
Last active July 3, 2020 11:08
Show Gist options
  • Save simon-tiger/eafcc0de763805c1908332aea47ea233 to your computer and use it in GitHub Desktop.
Save simon-tiger/eafcc0de763805c1908332aea47ea233 to your computer and use it in GitHub Desktop.
// If you don't like to go into callback hell and have lots of nested callbacks, here's a neat little program that will turn any function that takes a callback into a promise.
// NOTE: This assumes that the callback is the last argument
function promisify(func) {
return (...args) => {
return new Promise((resolve, reject) => func(...args, resolve));
}
}
function promisifyWithError(func) {
return (...args) => {
return new Promise((resolve, reject) => func(...args, (err, results) => {
if (err) reject(err);
else resolve(results);
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment