I have the following code to test asynchronous exectution in Javascript, using promises and timeout's:
'use strict'
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)
}
const opAProm = promisify(opA)
const opBProm = promisify(opB)
const opCProm = promisify(opC)
opAProm(print).then((res) => opBProm(print).then((res) => opCProm(print)))
The result I would expect is this:
A
B
C
But instead it gets printed just this:
A
I have looked for a solution and the most similar one I have found is this one although it hasn't help me really: Javascript Promises: then()'s aren't synchronous
So I would like to know how can I solve this, using promises or async/await. Thanks in advance!