Node.js Async Patterns & Error Handling — Questions and Answers
Question 1: What is the difference between Promises and callbacks in Node.js?
- Promises provide a cleaner syntax for handling async operations and support chaining; callbacks are the older pattern (Correct answer)
- There is no difference; they are interchangeable
- Callbacks are newer and more efficient than Promises
- Promises can only handle synchronous operations
Correct answer: Promises provide a cleaner syntax for handling async operations and support chaining; callbacks are the older pattern
Promises provide a cleaner, more manageable approach to asynchronous programming with .then()/.catch() chaining and avoid 'callback hell,' while callbacks are the original async pattern in Node.js.
Question 2: What does 'async/await' provide in Node.js?
- Syntactic sugar over Promises that makes asynchronous code look and behave like synchronous code (Correct answer)
- A way to make synchronous code run asynchronously automatically
- A method to block the event loop until an operation completes
- A replacement for the event loop entirely
Correct answer: Syntactic sugar over Promises that makes asynchronous code look and behave like synchronous code
async/await is syntactic sugar over Promises, allowing developers to write asynchronous code that reads like synchronous code, making it easier to understand and debug.
Question 3: How should unhandled Promise rejections be handled in a production Node.js application?
- By adding a global 'unhandledRejection' event handler and logging the error (Correct answer)
- By ignoring them since Node.js handles them automatically
- By wrapping every line of code in try-catch blocks
- By disabling Promises entirely in production
Correct answer: By adding a global 'unhandledRejection' event handler and logging the error
Production applications should listen for the 'unhandledRejection' event on the process object to catch and log any Promise rejections that were not handled, preventing silent failures.
Question 4: What is the 'error-first callback' pattern in Node.js?
- A convention where callbacks receive an error object as the first argument, null if no error occurred (Correct answer)
- A pattern where errors are always thrown as exceptions
- A method of logging errors to a file before callbacks execute
- A pattern where the first callback in a chain handles all errors
Correct answer: A convention where callbacks receive an error object as the first argument, null if no error occurred
The error-first callback pattern is a Node.js convention where callback functions receive an error object as their first argument (null if successful) and results as subsequent arguments.
Question 5: What is the purpose of 'try...catch' with async/await?
- To catch errors thrown by awaited Promises in a synchronous-looking style (Correct answer)
- To try different API endpoints until one works
- To cache the results of async operations
- To prevent the event loop from processing errors
Correct answer: To catch errors thrown by awaited Promises in a synchronous-looking style
try...catch blocks around await expressions catch rejected Promises and thrown errors, providing a familiar synchronous error-handling pattern for asynchronous operations.
Question 6: What happens if an error is thrown inside an async function without a try/catch?
- The returned Promise is rejected with the thrown error (Correct answer)
- The application crashes immediately
- The error is silently ignored
- Node.js automatically retries the function
Correct answer: The returned Promise is rejected with the thrown error
When an error is thrown inside an async function without try/catch, the Promise returned by the function is rejected. This rejection must be handled by .catch() or the caller's try/catch.
What is the difference between Promises and callbacks in Node.js?