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 loggers
const {promisify} = require('util');
const callbackFn = (firstName, callback) => {
setTimeout(() => {
console.log("1");
if (!firstName) callback(new Error('no first name passed in!'),null)
const fullName = `${firstName} IBM`
callback(null,fullName)
}, 2000)
}
async function useAwaitEx(){
try {
var calbbackfnpromisfied = promisify(callbackFn);
console.log("3");
var result = await calbbackfnpromisfied('mayank', console.log);
console.log("2");
console.log("result"+result)
}catch (error) {
console.log("error"+error);
}
}
useAwaitEx();
I am expecting output as 3 1 null 'mayank IBM' 2 null 'mayank IBM'
I am getting output as 3 1 null 'mayank IBM'