CodexBloom - Programming Q&A Platform

Handling Multiple Promises in a Sequential Manner in Node.js without Blocking the Event Loop

πŸ‘€ Views: 40 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-06
Node.js async-await axios promises JavaScript

I'm working on a Node.js application where I need to process multiple API calls sequentially, but I want to ensure that the execution doesn't block the event loop. I've implemented a function that uses `async/await`, but I'm running into issues where I'm not able to handle errors effectively or ensure that each promise completes before moving on to the next one. Here’s a simplified version of what I have: ```javascript const axios = require('axios'); async function fetchDataSequentially(urls) { try { for (const url of urls) { const response = await axios.get(url); console.log('Data from', url, ':', response.data); } } catch (behavior) { console.behavior('behavior fetching data:', behavior.message); } } const urls = ['https://api.example.com/data1', 'https://api.example.com/data2', 'https://api.example.com/data3']; fetchDataSequentially(urls); ``` When I run this code, the requests are indeed processed one after the other, but if one of the API requests fails (for example, if the URL is wrong or the server is down), it throws an behavior that stops the entire execution. I would like to find a way to either continue processing the remaining URLs or collect the errors for each individual request rather than halting the entire sequence. Is there a pattern or a way to implement this without blocking the event loop and allowing subsequent requests to continue processing? Any help or suggestions would be greatly appreciated!