Skip to content

Instantly share code, notes, and snippets.

@abelyakin
Created June 27, 2020 19:31
Show Gist options
  • Save abelyakin/c377dbfadb0064206eb8184360f46368 to your computer and use it in GitHub Desktop.
Save abelyakin/c377dbfadb0064206eb8184360f46368 to your computer and use it in GitHub Desktop.
Promisify func
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