How Promise.all Works in a Single-Threaded JavaScript World

2026-11-04来源 dev.to JavaScriptEnglish

How Promise.all Works in a Single-Threaded JavaScript World

If JavaScript is single-threaded, how can Promise.all() handle multiple requests at the same time?

The short answer:

JavaScript is single-threaded for executing code, but I/O is handled outside the JS thread.

🧠 What “single-threaded” really means

  • Only one piece of JavaScript runs at a time.
  • JS does NOT block while waiting for network, timers, or disk.

⚙️ Step-by-step: What happens with Promise.all

await Promise.all([ fetch("/a"), fetch("/b"), fetch("/c"), ]); 

1️⃣ Requests start immediately

const p1 = fetch("/a"); const p2 = fetch("/b"); const p3 = fetch("/c"); 
  • Promises are created instantly.
  • Network work starts in the background.
  • JS thread is free.

2️⃣ Network runs outside JS

  • Browser or Node runtime handles requests.
  • Requests run concurrently.
  • JS thread does nothing during this time.

3️⃣ Promise resolution uses microtasks

  • When a request finishes, its Promise resolves.
  • .then() callbacks go to the microtask queue.
  • JS executes them one by one.

4️⃣ Promise.all resolves last

  • Waits for all Promises to resolve.
  • Rejects early if one fails.

🔄 Timeline

JS Thread: start A → start B → start C → (free) → run handlers ├─ Network: A request → A done ├─ Network: B request → B done └─ Network: C request → C done 

🧠 Mental Model

  • JS thread = single chef
  • Network = multiple delivery drivers
  • Promise.all = wait for all deliveries

⚠️ Important note

Promise.all() does NOT make CPU work parallel.

Promise.all([ heavyTask(), heavyTask(), ]); 

This still blocks the UI.

🧱 Browser vs Node

Layer Responsibility
JS Thread Executes callbacks
Browser APIs / libuv Async I/O
Promises Coordination

✅ Summary

Concept Meaning
Single-threaded One JS execution at a time
Concurrent Multiple async ops in flight
Promise.all Waits for all async results
Parallel JS ❌
Parallel I/O ✅

✨ Final takeaway

JS coordinates work — it doesn’t do it all itself.

Promise.all works because the heavy work happens outside the JS thread.

Originally published on Bitlyst.

原文:https://www.a8q9.cn/en/article/how-promise-all-works-in-a-single-threaded-javascript-world-a97969.html

相关文章