Created
December 8, 2020 17:07
-
-
Save rajatjain-21/2afee205e9cec0558acc312f944cf8d5 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
function normalFunc(x,y,callback) { | |
const result = x + y; | |
callback(result); | |
} | |
async function run() { | |
const promisedFn = promisify(normalFunc); | |
const result = await promisedFn(4,3).then(data => data*data); | |
console.log(result); | |
} | |
run() | |
function promisify(fn) { | |
return (...args) => { | |
return new Promise((resolve) => { | |
function cb(val) { | |
resolve(val) | |
} | |
fn.apply(this, args.concat(cb)); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment