When working with APIs, it's often necessary to request data from multiple endpoints at the same time. This is where parallel promises come in. Parallel promises allow developers to make multiple API requests simultaneously, rather than waiting for each request to finish before making the next one. This can greatly improve the speed and efficiency of data retrieval.

To execute multiple promises in parallel, developers can use the Promise.all() method. This method takes an array of promises as its argument, and returns a new promise that resolves with an array of the resolved values from each promise in the original array, in the order they were passed. If any of the promises in the original array reject, the new promise returned by Promise.all() will reject with the first rejection error.

function task (name) {
    console.info(`Running "${name}"`);
    
    return new Promise(resolve => {
        let wait = Math.round(Math.random() * 1000);
    
        setTimeout(() => {
            console.info(`Completed "${name}" in ${wait} ms`);
            resolve();
        }, wait);
    });
}

async function multiTasks () {
    let task1 = task("1"),
        task2 = task("2");
        
    await task1;
    await task2;
    
    console.info("All done");
}

async function multiTasks2 () {
    let tasks = Promise.all([task("3"), task("4")]);
        
    await tasks;
    
    console.info("All done");
}

multiTasks();
multiTasks2();

await Promise.all(items.map(async (item) => { 
  await fetchItem(item) 
}))

Resources