node.js How asynchronous are Async loops really?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A JavaScript loop is still a normal synchronous loop unless the code inside it explicitly waits or schedules asynchronous work. The confusing part is that different loop patterns interact with await very differently, so “async loop” can mean serial execution, parallel task creation, or a broken pattern that does not wait at all.
for...of with await Runs Serially
If you write await inside a for...of loop, each iteration waits before the next one starts:
This is asynchronous in the sense that each task can perform non-blocking I/O, but the loop itself advances one iteration at a time.
forEach Does Not Await the Callback
A very common mistake is using forEach with an async callback:
forEach does not wait for those async callbacks to finish. The outer function can continue or finish before the tasks complete, which surprises many developers.
Promise.all Starts Work in Parallel
If you want to launch many async operations and wait for all of them together, create the promises and use Promise.all:
This is often what people expect when they ask how asynchronous a loop really is. The loop that creates promises is synchronous, but the operations those promises represent can progress concurrently.
The Loop Is Not the Asynchronous Part
This is the core idea:
- the loop structure itself is ordinary synchronous control flow
- the async operations inside it yield back to the event loop when they await I/O or promises
- your chosen pattern decides whether iterations wait one by one or overlap
So “async loop” is really shorthand for “a loop that creates or awaits asynchronous work,” not a special category of loop implemented by Node.js.
Pick the Pattern by Intent
Use:
- '
for...ofwithawaitfor ordered serial processing' - '
Promise.allfor parallel work when order of completion does not matter' - controlled concurrency tools when fully parallel execution would be too much
That last case matters in real systems, because “parallelize everything” can overwhelm databases, APIs, or the local process.
Concurrency Limits Sit Between the Extremes
Many production loops need something between fully serial await and unrestricted Promise.all. A concurrency limiter lets several tasks run at once without allowing the loop to flood downstream services.
Throughput and Ordering Are Different Goals
Some loops need strict ordering, while others only need good throughput. Make that choice explicitly, because the correct loop pattern depends on whether the next iteration may start before the previous one finishes.
Common Pitfalls
- Using
forEachwith async callbacks and expecting it to wait. - Calling a loop “asynchronous” without deciding whether the work should be serial or parallel.
- Launching too many concurrent promises at once with
Promise.all. - Assuming
awaitmakes the loop itself parallel. Infor...of, it usually makes it sequential. - Forgetting that CPU-bound work still blocks JavaScript even inside async functions.
Summary
- Loops in Node.js are ordinary synchronous control flow.
- '
awaitchanges how work is sequenced, not the nature of the loop construct itself.' - '
for...ofwithawaitis usually serial.' - '
forEachdoes not await async callbacks.' - '
Promise.allis the common tool when you want overlapping asynchronous work.'
Related reading
- Node.js How do you handle callbacks in a loop?
- Node.js Is there a synchronous version of the http.get method in node.js?
- Node.js maxing out at 1000 concurrent connections
- node.js resolve promise and return value
- NodeJS How do I Download a file to disk from an aws s3 bucket?
- Nodejs how to update client after external program has returned?
- Node.js Streams onend completing before asynchronous onreadable completed
- node.js vs. asp.net async pages
.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.