🔰await in parallel

JSasyncawaitpromises ⟩ in parallel

await promises in parallel.

async function g() {
    const p1 = futureValue(1, 2);    // in 2 sec
    const p2 = futureValue(2, 4);    // in 4 sec
    return await p1 + await p2;                     // ⭐ await in parallel
}

async function h() {
    const p1 = futureValue(7, 2);    // in 2 sec
    const p2 = futureValue(8, 4);    // in 4 sec
    const [r1, r2] = await Promise.all([p1, p2]);    // ⭐ await in parallel
    return r1 + r2;
}
  • results

measureTime(g);        // ⭐ 4.00 sec : g() -> 3
measureTime(h);        // ⭐ 4.00 sec : h() -> 15

Last updated