JavaScript Q&A Logo
JavaScript Q&A Part of the Q&A Network

How can I use async/await to handle multiple fetch requests in parallel?

Asked on Oct 08, 2025

Answer

You can use "async/await" in combination with "Promise.all" to handle multiple fetch requests in parallel. This approach allows you to wait for all promises to resolve before proceeding.
<!-- BEGIN COPY / PASTE -->
        async function fetchMultipleUrls(urls) {
            try {
                const fetchPromises = urls.map(url => fetch(url));
                const responses = await Promise.all(fetchPromises);
                const dataPromises = responses.map(response => response.json());
                return await Promise.all(dataPromises);
            } catch (error) {
                console.error("Error fetching data:", error);
            }
        }

        const urls = ["https://api.example.com/data1", "https://api.example.com/data2"];
        fetchMultipleUrls(urls).then(data => console.log(data));
        <!-- END COPY / PASTE -->
Additional Comment:
  • The "fetchMultipleUrls" function takes an array of URLs and maps each URL to a fetch promise.
  • "Promise.all" is used to wait for all fetch requests to complete.
  • The responses are then mapped to "response.json()" promises to extract JSON data.
  • Another "Promise.all" is used to wait for all JSON data to be resolved.
  • Errors are caught and logged using a try-catch block.
✅ Answered with JavaScript best practices.
← Back to All Questions