Interview question
How does JavaScript handle concurrency without multiple threads? JavaScript multiple threads के बिना concurrency कैसे handle करता है?
Answer
JavaScript achieves concurrency through non-blocking I/O and the event loop, rather than through true parallel execution on multiple threads. Long-running work is delegated elsewhere while JS itself stays single-threaded.
Where the work actually happens:
JS engine (single thread) → delegates I/O-bound work (timers, network, file access) to the browser/Node's C++ APIs or a thread pool → those run truly in parallel, outside JS → results come back as callbacks/Promises via the event loop
JS engine (single thread) → delegates I/O-bound work (timers, network, file access) to the browser/Node's C++ APIs or a thread pool → those run truly in parallel, outside JS → results come back as callbacks/Promises via the event loop
// This looks like it's doing multiple things 'at once', but JS itself
// never runs two lines of YOUR code simultaneously
console.log('Start');
setTimeout(() => console.log('Timer done'), 1000); // delegated to browser timer thread
fetch('/api/data').then(() => console.log('Fetch done')); // delegated to network stack
fs.readFile('file.txt', () => console.log('File read done')); // Node: delegated to libuv thread pool
console.log('End');
// 'Start' and 'End' run synchronously first; the three async operations
// run truly concurrently OUTSIDE the JS thread, and their callbacks
// are queued back into JS one at a time as each completes
// CPU-bound work does NOT benefit from this model - it still blocks
function heavyComputation() {
let result = 0;
for (let i = 0; i < 1e9; i++) result += i; // pure CPU work, blocks the thread
return result;
}
// heavyComputation() would freeze everything - no Web API can help here
// since there's no I/O to delegate, it's pure computation
// For true parallelism with CPU-bound work, use Web Workers (browser)
// or worker_threads (Node.js) - these run on SEPARATE threads entirely
const worker = new Worker('heavy-task.js');
worker.postMessage('start');
worker.onmessage = (e) => console.log('Result from worker:', e.data);
// Main thread stays responsive while the worker computes in parallelJavaScript non-blocking I/O और event loop के ज़रिए concurrency achieve करता है, multiple threads पर true parallel execution से नहीं। Long-running काम कहीं और delegate होता है जबकि JS खुद single-threaded रहता है।
काम असल में कहाँ होता है:
JS engine (single thread) → I/O-bound काम browser/Node के C++ APIs या thread pool को delegate करता है → वो truly parallel चलते हैं → results callbacks/Promises की तरह event loop के ज़रिए वापस आते हैं
JS engine (single thread) → I/O-bound काम browser/Node के C++ APIs या thread pool को delegate करता है → वो truly parallel चलते हैं → results callbacks/Promises की तरह event loop के ज़रिए वापस आते हैं
console.log('Start');
setTimeout(() => console.log('Timer done'), 1000); // browser timer thread को delegate
fetch('/api/data').then(() => console.log('Fetch done')); // network stack को delegate
fs.readFile('file.txt', () => console.log('File read done')); // Node: libuv thread pool
console.log('End');
// 'Start' और 'End' पहले sync चलते हैं; तीनों async operations JS thread
// के बाहर truly concurrently चलते हैं, callbacks एक-एक करके वापस आते हैं
// CPU-bound काम इस model से फायदा नहीं उठाता - अभी भी block करता है
function heavyComputation() {
let result = 0;
for (let i = 0; i < 1e9; i++) result += i; // pure CPU काम, thread block करता है
return result;
}
// True parallelism के लिए CPU-bound काम में Web Workers use करें
const worker = new Worker('heavy-task.js');
worker.postMessage('start');
worker.onmessage = (e) => console.log('Result from worker:', e.data);
// Main thread responsive रहता है जबकि worker parallel compute करता हैWas this answer clear?