Using Node Js Utility.promisify() Call Is Not Coming Back From Async Callback Method
I am trying to use node js promisify to convert the callback to promise inoreder to use await on a async callback function. I tried different ways to pass the parameter and put log
Solution 1:
I think a very small change should fix this.
If we want to pass console.log as a parameter we need to declare it separately in the callbackFn function.
The key point here is that we shouldn't pass the last callback to a promisified function when we call it, this will be handled for us.
We won't log exactly what you wish, because result will not be null.
We'll log:
31null'mayank IBM'2result: mayank IBM
Updated code:
const {promisify} = require('util');
constcallbackFn = (firstName, logCallback, callback) => {
setTimeout(() => {
console.log("1");
if (!firstName) callback(newError('no first name passed in!'),null)
const fullName = `${firstName} IBM`logCallback(null,fullName)
callback(null,fullName)
}, 2000)
}
asyncfunctionuseAwaitEx(){
try {
var calbbackfnpromisfied = promisify(callbackFn);
console.log("3");
var result = awaitcalbbackfnpromisfied('mayank', console.log);
console.log("2");
console.log("result:", result)
// console.log("result"+result)
}catch (error) {
console.log("error"+error);
}
}
useAwaitEx();
Post a Comment for "Using Node Js Utility.promisify() Call Is Not Coming Back From Async Callback Method"