MEAN Node.js Async Patterns and Performance 2 — Questions and Answers
Question 1: What is the purpose of Node.js streams in a MEAN stack application?
- Process data in chunks for memory efficiency, ideal for large file uploads or database exports (Correct answer)
- Stream real-time updates to Angular clients via WebSockets
- Pipe data between MongoDB aggregation stages
- Chain Express middleware functions together
Correct answer: Process data in chunks for memory efficiency, ideal for large file uploads or database exports
Streams process data incrementally in chunks rather than loading it all into memory, making them essential for handling large files, CSV exports, or proxying requests.
Question 2: What does `async/await` syntax do in Node.js?
- Makes asynchronous Promise-based code look synchronous, improving readability and error handling (Correct answer)
- Creates genuinely synchronous code that blocks execution
- Converts callback-based functions to Promises automatically
- Runs code on a background thread transparently
Correct answer: Makes asynchronous Promise-based code look synchronous, improving readability and error handling
async/await is syntactic sugar over Promises that lets you write asynchronous code in a linear, readable style, with try/catch for error handling instead of .catch().
Question 3: What is the Node.js `EventEmitter` class used for?
- Implements the observer pattern allowing objects to emit named events that listeners can respond to (Correct answer)
- Emits HTTP events from Express to Angular clients
- Manages DOM events on the server side
- Schedules recurring tasks in the event loop
Correct answer: Implements the observer pattern allowing objects to emit named events that listeners can respond to
EventEmitter lets you create custom event-driven architectures; objects emit named events and registered listeners are called asynchronously.
Question 4: What is `util.promisify()` used for in Node.js?
- Converts callback-style functions following the error-first pattern into Promise-returning functions (Correct answer)
- Validates that a function returns a Promise
- Creates utility Promises for common async patterns
- Adds timeout capability to existing Promises
Correct answer: Converts callback-style functions following the error-first pattern into Promise-returning functions
util.promisify() wraps legacy Node.js callback-based functions (like fs.readFile) into functions that return Promises, enabling async/await usage.
Question 5: What is backpressure in Node.js streams and why does it matter?
- When a writable stream can't consume data as fast as the readable produces it, and the readable should pause (Correct answer)
- When database writes create pressure on the event loop
- When HTTP responses are too large for the TCP buffer
- When too many concurrent requests queue up in Express
Correct answer: When a writable stream can't consume data as fast as the readable produces it, and the readable should pause
Backpressure occurs when a consumer can't keep up with a producer; proper handling via the 'drain' event prevents unbounded memory growth in streaming pipelines.
Question 6: What is the difference between `require()` and ES module `import` in Node.js?
- require() is synchronous CommonJS; import is asynchronous ES modules with static analysis and tree-shaking support (Correct answer)
- require() is for npm packages; import is for local files only
- require() loads at startup; import loads on demand always
- Both are identical, import is just newer syntax
Correct answer: require() is synchronous CommonJS; import is asynchronous ES modules with static analysis and tree-shaking support
require() uses CommonJS and loads synchronously; ES module import is parsed statically at compile time, enabling tree-shaking and top-level await in Node.js.
What is the purpose of Node.js streams in a MEAN stack application?