AceDevHub
Intermediate JavaScript Interview QuestionsIntermediatePractical

JavaScript · Question 49

How do you avoid accidentally running independent asynchronous operations sequentially with await?

Direct answer

If operations are independent, start them before awaiting or coordinate them with a Promise combinator; awaiting each operation before starting the next creates sequential latency.

This code is sequential: const a = await fetchA(); const b = await fetchB();. fetchB() is not even started until fetchA() has completed.

For independent work, you can start both first: const pa = fetchA(); const pb = fetchB(); const [a,b] = await Promise.all([pa,pb]);. Total latency can then approach the slower operation rather than the sum of both latencies.

  • Use sequential awaits when the next operation genuinely depends on the previous result.
  • Use concurrent coordination for independent operations when the downstream system can safely handle the concurrency.
  • For hundreds or thousands of operations, unlimited Promise.all() may overload APIs, databases, memory, or rate limits; use bounded concurrency instead.