Node.js FAQ: Top Questions
3. What is the difference between synchronous and asynchronous code in Node.js?
Synchronous code executes sequentially, blocking further execution until complete, while asynchronous code allows non-blocking operations, using callbacks, promises, or async/await.
- Synchronous: Simple but can slow down I/O-heavy tasks.
- Asynchronous: Handles I/O efficiently via the event loop.
-
Examples:
fs.readFileSync(sync) vs.fs.readFile(async). - Use Case: Async for I/O (network, files); sync for quick tasks.
