Interview Q&A / Node.js
Node.js interview questions
The event loop on the server, streams, modules and building APIs with Express.
Prefer rapid revision? Practise Node.js short interview answers →
- How do you handle errors in asynchronous Node.js code, and what is an unhandled promise rejection?
Use try/catch around await, .catch() on promise chains, and the error-first argument in callbacks. An unhandled rejection is a promise that fails with no handler attached; since Node 15 it crashes the process by default, so every promise needs a catch somewhere.
IntermediateIn-depth3 min read - What is the difference between process.nextTick, setImmediate and setTimeout in Node.js?
process.nextTick runs right after the current operation, before promise callbacks and before the event loop moves on. setImmediate runs in the check phase, right after I/O polling. setTimeout(fn, 0) runs in the timers phase after at least about 1 ms.
IntermediateIn-depth3 min read - What is callback hell and how do you avoid it?
Callback hell is deeply nested callbacks where each step lives inside the previous one, making code hard to read and handle errors in. Avoid it with named functions, promises, and async/await.
BeginnerQuick answer1 min read - What is the difference between fs.readFileSync, fs.readFile and fs.promises.readFile?
readFileSync blocks the thread until done. readFile takes a callback and does not block. fs.promises.readFile returns a promise for use with await. In servers, prefer the non-blocking forms.
BeginnerQuick answer1 min read - What is the difference between Promise.all and Promise.allSettled?
Promise.all rejects as soon as any promise rejects and gives you results only if all succeed. Promise.allSettled always waits for every promise and reports each outcome as fulfilled or rejected.
BeginnerQuick answer1 min read - Predict the output: setTimeout, a promise, process.nextTick and plain logs together
Synchronous code runs first, then process.nextTick callbacks, then promise callbacks (microtasks), then timers. So the order is sync, nextTick, promise, setTimeout.
IntermediateQuick answer1 min read - Should you use await inside a for loop or Promise.all for independent tasks?
For independent tasks use Promise.all with map so they run concurrently. await inside a loop runs them one by one, which is right only when each step depends on the last or order and rate limits matter.
IntermediateQuick answer1 min read - What is top-level await and where can you use it in Node.js?
It lets you use await directly at the top of a file without wrapping it in an async function. It works only in ES modules, and it delays anything that imports that module until the promise settles.
IntermediateQuick answer1 min read