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:
✅ Answered with JavaScript best practices.- 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.
Recommended Links:
← Back to All Questions