How to use await in a loop
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Using await in a loop is valid JavaScript, but the correct loop style depends on whether tasks must run sequentially or can run in parallel. Many bugs come from mixing those two goals without realizing it. The right question is not "can I use await in a loop". The right question is "do I want each iteration to wait for the previous one".
Use for...of for Sequential Async Work
If the operations must happen in order, for...of with await is the cleanest option.
This waits for each request to finish before starting the next one. That is correct when:
- order matters
- the server should not be flooded with parallel requests
- later work depends on earlier results
Do Not Expect forEach to Await Properly
This is a common mistake:
forEach does not await the async callback. The surrounding function continues immediately, so this pattern often leads to unfinished work, confusing logs, or uncaught timing assumptions.
If you need sequencing, use for...of. If you need parallelism, build promises explicitly.
Use Promise.all for Parallel Work
If iterations are independent, parallel execution is often faster.
This starts all requests immediately and waits for all of them. It is usually the right pattern when order of completion does not matter and the workload can safely run concurrently.
Use Promise.allSettled When Partial Failure Is Acceptable
Promise.all rejects as soon as one promise rejects. If you want to collect every result, including failures, use Promise.allSettled.
This is useful for dashboards, batch jobs, and cleanup operations where one failure should not hide the rest.
For workloads with external APIs, this often produces a better operational picture than failing fast, because you can log which calls failed while still returning the successful results.
The same pattern is useful for cleanup jobs, migration scripts, and dashboards where "best effort plus reporting" is more valuable than one all-or-nothing failure.
for await...of Is for Async Iterables
for await...of is a different feature. It is meant for async iterables or streams, not ordinary arrays of promises by default.
This is great for paginated APIs, streaming sources, or generator-based pipelines.
Choose by Concurrency Intent
A practical rule:
for...ofplusawaitfor sequential workPromise.allfor parallel workPromise.allSettledfor parallel work with tolerated failuresfor await...offor async iterables
Most confusion disappears once the concurrency model is chosen explicitly.
Common Pitfalls
- Using
awaitinsideforEachand expecting the outer flow to wait. - Running sequential loops when the work could safely run in parallel.
- Using
Promise.allwhen one failure should not cancel the whole batch. - Confusing arrays of promises with async iterables.
- Forgetting that parallelism can overload a server or API if not controlled.
Summary
- '
awaitin a loop is fine when sequential behavior is intentional.' - Use
for...offor ordered async work. - Use
Promise.allorPromise.allSettledfor parallel tasks. - Use
for await...ofonly for async iterables and streams. - Pick the loop pattern based on concurrency semantics, not syntax preference.
Related reading
- How to use await in a python lambda
- How to use background thread in swift?
- How to use background thread in swift?
- How to use background thread in swift?
- How to use begins_with in AWS DynamoDb js sdk?
- How to use Google API Client Library for JavaScript gapi with async/await?
- How to use C8 IAsyncEnumerableT to async-enumerate tasks run in parallel
- How to use Callable with void return type?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.