Node.js Async Patterns & Error Handling 2 — Questions and Answers
Question 1: What does `Promise.allSettled()` return when some promises reject?
- It rejects immediately with the first error
- It resolves with an array of outcome objects for all promises (Correct answer)
- It resolves with only the fulfilled values
- It throws a synchronous error
Correct answer: It resolves with an array of outcome objects for all promises
`Promise.allSettled()` always resolves with an array of objects each having `status` ('fulfilled' or 'rejected') and either `value` or `reason`.
Question 2: In Node.js, what is the correct way to handle errors in an async/await function?
- Use `.catch()` on the function call
- Wrap await expressions in try/catch blocks (Correct answer)
- Listen for the 'error' event on the function
- Use process.on('uncaughtException')
Correct answer: Wrap await expressions in try/catch blocks
In async/await code, try/catch blocks are the idiomatic way to catch rejected promises.
Question 3: What happens if you `await` a non-Promise value in Node.js?
- It throws a TypeError
- It wraps the value in a resolved Promise and returns it (Correct answer)
- It blocks the event loop indefinitely
- It returns undefined
Correct answer: It wraps the value in a resolved Promise and returns it
`await` on a non-thenable value is equivalent to `await Promise.resolve(value)` and simply returns the value.
Question 4: Which Node.js utility converts callback-based functions into Promise-returning functions?
- util.promisify() (Correct answer)
- util.callbackify()
- util.asyncify()
- util.wrap()
Correct answer: util.promisify()
`util.promisify()` wraps a Node.js-style (err, value) callback function and returns a version that returns a Promise.
Question 5: What is the output order of the following code? ``` console.log('A'); setTimeout(() => console.log('B'), 0); Promise.resolve().then(() => console.log('C')); console.log('D'); ```
- A, B, C, D
- A, D, B, C
- A, D, C, B (Correct answer)
- A, C, D, B
Correct answer: A, D, C, B
Synchronous code (A, D) runs first, then microtasks like Promise callbacks (C), then macrotasks like setTimeout (B).
Question 6: How can you run multiple async operations concurrently and wait for all to complete?
- await each operation sequentially in a for loop
- Use Promise.all() with an array of promises (Correct answer)
- Use setTimeout with a fixed delay
- Chain .then() calls on a single promise
Correct answer: Use Promise.all() with an array of promises
`Promise.all()` starts all promises simultaneously and resolves when all succeed, or rejects on the first failure.
Question 7: What is 'callback hell' in Node.js?
- When callbacks are called before the event loop starts
- Deeply nested callbacks that make code hard to read and maintain (Correct answer)
- A Node.js error when too many callbacks are registered
- When a callback is accidentally called twice
Correct answer: Deeply nested callbacks that make code hard to read and maintain
Callback hell refers to deeply nested, hard-to-read code that arises when many async operations depend on each other via callbacks.
What does `Promise.allSettled()` return when some promises reject?