Why is it a problem to await inside a loop, and what is the alternative?

Node.js modules, files and async · Freshers and experienced

Model answer

Awaiting each item in a for loop runs them one after another, which is slow if the tasks are independent. Start them together and wait: await Promise.all(items.map(fn)). Keep sequential awaits when order matters or to limit load, and for very many items use a concurrency limit.

Learn this topic

Async JavaScript in Node: Promises, async/await and the Event Loop in the course Node.js and Express: Build a Back End API from Scratch.

Sign in to add your own answer.

More questions