Last active
July 3, 2020 11:08
-
-
Save simon-tiger/eafcc0de763805c1908332aea47ea233 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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