Handling Multiple Promises with Promise.all()

by dshilkret

HTML

<div>
Promise.all() allows you to run multiple promises concurrently. The await keyword waits for both promises to resolve before moving on. This is useful for situations where you need to wait for multiple async operations to complete.
</div>

JavaScript

const promise1 = new Promise(resolve => setTimeout(() => resolve("First promise"), 1000));
const promise2 = new Promise(resolve => setTimeout(() => resolve("Second promise"), 2000));

const runMultiplePromises = async () => {
    const results = await Promise.all([promise1, promise2]);
    console.log("All promises resolved:", results);
};

runMultiplePromises();