Created
June 27, 2020 19:31
-
-
Save abelyakin/c377dbfadb0064206eb8184360f46368 to your computer and use it in GitHub Desktop.
Promisify func
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
function promisify(func) { | |
// A new function which returns promise | |
return function (...args) { | |
return new Promise((resolve, reject) => { | |
// Create our custom callback function which will original function will call | |
let callback = function (err, response) { | |
// On recieving callback it will resolve or reject the promise | |
if (err) return reject(err); | |
return resolve(response); | |
} | |
args.push(callback); | |
func.call(this, ...args); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment