Answer the question
In order to leave comments, you need to log in
How much is the execution time of two promises sequentially equivalent to the parallel execution time?
I want to execute two promises sequentially, but the result of the time is the same as with parallel execution. What am I doing wrong?
const getA = new Promise((resolve, reject) => {
console.time('getA');
setTimeout(() => {
console.timeEnd('getA');
resolve(4)
}, 6000)
});
const getB = new Promise((resolve, reject) => {
console.time('getB');
setTimeout(() => {
console.timeEnd('getB');
resolve(2)
}, 2000)
});
console.time('get sum');
getA.then(a => {
getB.then(b => {
console.log(`result: ${a + b}`);
console.timeEnd('get sum');
})
});
Answer the question
In order to leave comments, you need to log in
time result as in parallel execution
const getA = () => new Promise((resolve, reject) => {
console.time('getA');
setTimeout(() => {
console.timeEnd('getA');
resolve(4)
}, 6000)
});
const getB = () => new Promise((resolve, reject) => {
console.time('getB');
setTimeout(() => {
console.timeEnd('getB');
resolve(2)
}, 2000)
});
console.time('get sum');
getA().then(a => {
getB().then(b => {
console.log(`result: ${a + b}`);
console.timeEnd('get sum');
})
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question