Starting multiple async/await functions at once and handling them separately
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In JavaScript, running async tasks sequentially is often slower than necessary. A better pattern is starting multiple promises at once, then awaiting or handling each result according to business rules. The key is separating task kickoff from task consumption so you control concurrency and error behavior.
Start Tasks First, Await Later
await pauses the current function, so awaiting too early accidentally serializes work. Start all operations first, store promises, then await them when results are needed.
Even though results are consumed at different times, all requests started immediately.
Handling Results Separately
If each task has independent fallback logic, use individual try and catch blocks.
This pattern avoids one failure cancelling unrelated successful data.
Use Promise.allSettled for Batch Reporting
When you need complete success and failure information together, Promise.allSettled is cleaner.
allSettled is ideal for jobs where partial completion is acceptable.
Concurrency Limits
Starting everything at once can overload APIs or local resources. Use a small worker pool when tasks are numerous.
This keeps throughput high without opening unlimited concurrent operations.
Cancellation and Timeouts
Long-running async operations should support cancellation and timeout boundaries. In browser and recent Node environments, AbortController is the standard approach for fetch calls.
Without timeout logic, one stalled request can block downstream awaits and degrade user experience.
Ordered Versus Fastest Consumption
Some workflows require strict output order for auditing and reproducible logs. Others should render partial data immediately as each promise resolves. Choose this behavior explicitly so your UI and observability patterns remain predictable under load.
Common Pitfalls
- Awaiting each call immediately after starting it. Fix by creating all promises first.
- Using
Promise.allwhen partial success is acceptable. Fix by choosingallSettledor per-promise handling. - Ignoring concurrency limits for large batches. Fix by adding a worker-pool pattern.
- Letting one rejected promise crash an unrelated workflow. Fix by isolating errors around each awaited result.
- Forgetting cancellation and timeout control. Fix by adding
AbortControllerand explicit timeout wrappers.
Summary
- Kick off async work early and await results strategically.
- Handle each promise independently when failure policies differ.
- Use
Promise.allSettledfor complete batch outcome reporting. - Limit concurrency to protect APIs and app stability.
- Add timeout and cancellation support for production reliability.
Related reading
- STAThread and multithreading
- STAThread and multithreading
- Static method behavior in multi-threaded environment in java
- stdasync function running serially
- Static variables in JavaScript
- Stop Safari Mobile from giving input buttons rounded corners
- stdasync won't spawn a new thread when return value is not stored
- stdlock_guard or stdscoped_lock?
.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.