I have the following exercise:
const { promisify } = require('util')
const print = (err, contents) => {
if (err) console.error(err)
else console.log(contents)
}
const opA = (cb) => {
setTimeout(() => {
cb(null,'A')
}, 500)
}
const opB = (cb) => {
setTimeout(() => {
cb(null, 'B')
}, 250)
}
const opC = (cb) => {
setTimeout(() => {
cb(null, 'C')
}, 125)
}
Without modifying the functions and the cb is it possible to use promisify to transform it into promise based?
UPDATE. I try this solution but it doesn't work as I expect. The print sequence is C B A, while I was expecting A B C
const promA = promisify(opA)
const promB = promisify(opB)
const promC = promisify(opC)
promA(print).then(promB(print)).then(promC(print))
UPDATE II. Solved, thanks everyone :
const promiseA = promisify(opA);
const promiseB = promisify(opB);
const promiseC = promisify(opC);
Promise.all([promiseA(),promiseB(),promiseC()]).then((res)=>{
res.map(val => print(null,val))
})