js-vault
js-vault copied to clipboard
Promisify a function
Given a function as mentioned below. Write a utility, promisify, where the usage is as explained below
function asyncFunction(a, b, callback) {
const isError = Math.random() > 0.7;
setTimeout(() => {
if (isError) {
callback(null, new Error("Something went wrong"));
return;
}
callback(a + b, null);
}, 3000);
}
function promisify(fn) {
// Implement Code here
}
// Old ussage
asyncFunction(1,2, function(result, err) {
console.log(result,err)
})
// New Usage
const promisifiedFn = promisify(asyncFunction);
promisifiedFn(1, 2)
.then(res => {
console.log("result", res);
})
.catch(console.log);
I am interested to work on this. Can I proceed?
If yes, am I supposed to add the same in src/content/promisify/promisify.mdx ?
Yea works also add the route in routes.js
Sure