Junior
Determining the minimum execution time of asynchronous functions
livecode
Task condition
Given three functions. The asynchronous functions add1() and add2() perform addition operations differently. Determine the minimum time for which add1() and add2() functions will execute.
function resolveAfter2Seconds(x) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function add1(x) {
const a = await resolveAfter2Seconds(20);
const b = await resolveAfter2Seconds(30);
return x + a + b;
}
async function add2(x) {
const promise_a = resolveAfter2Seconds(20);
const promise_b = resolveAfter2Seconds(30);
return x + (await promise_a) + (await promise_b);
}
add1(10).then(console.log);
add2(20).then(console.log);