EXERCISE
1JavaScript uses a concurrency model based on the event loop. It is not truly parallel, but it feels concurrent because it switches between tasks very quickly.
Save
is single-threaded but concurrent. It cannot do two things at the same time, but it can juggle many things by switching between them fast enough that it feels simultaneous.
How JS handles multiple async operations:
console.log("Fetching data...");
setTimeout(function() {
console.log("Timer 1 done");
}, 1000);
setTimeout(function() {
console.log("Timer 2 done");
}, 2000);
setTimeout(function() {
console.log("Timer 3 done");
}, 3000);
console.log("All timers set!");
// Output:
// Fetching data...
// All timers set!
// Timer 1 done (after 1s)
// Timer 2 done (after 2s)
// Timer 3 done (after 3s)
// All three timers run "at the same time" in the browser
// But their callbacks run one at a time on the JS call stack
The illusion:
// What it FEELS like:
// Timer 1, Timer 2, Timer 3 all counting simultaneously
// What ACTUALLY happens:
// 1. JS sends all three timer requests to the browser
// 2. Browser runs three timers in parallel (browser is multi-threaded!)
// 3. As each timer finishes, browser puts callback in the queue
// 4. Event loop picks them up one at a time
// 5. Each callback runs to completion before the next one starts
// JS is single-threaded. The browser is not.
// The browser does the parallel work.
// JS just processes the results one by one.
> Key Insight: JavaScript itself never does two things at once. But the browser can run multiple timers, network requests, and other async operations in parallel. JavaScript just processes their results one at a time through the event loop. This design is why JavaScript can handle thousands of concurrent connections in .