This example runs two async tasks in sequence and catches errors from either.
Node.js
async function processTasks() {
try {
const result1 = await task1();
const result2 = await task2();
console.log(result1, result2);
} catch (error) {
console.error('One of the tasks failed:', error);
}
}
Sample Program
This program tries to read a JSON config file asynchronously. If the file is missing or contains invalid JSON, it catches the error and prints a clear message.
The rejected promise throws an error caught by the catch block.
Step 2: Check console.log output
The catch block logs 'Caught:' followed by the error message 'fail'.
Final Answer:
Caught: fail -> Option B
Quick Check:
Error caught and logged = B [OK]
Hint: Rejected promises inside try are caught by catch [OK]
Common Mistakes:
Expecting unhandled rejection error
Thinking error is logged without 'Caught:' prefix
Assuming no output because of rejection
4. Identify the error in this async function error handling code:
async function load() {
try {
const data = await fetchData();
} catch {
console.error(error);
}
}
medium
A. console.error cannot log variables
B. Await cannot be used inside try block
C. fetchData() must be awaited outside try
D. Missing error parameter in catch block
Solution
Step 1: Check catch block syntax
The catch block is missing the error parameter to receive the thrown error.
Step 2: Understand console.error usage
console.error(error) references a variable 'error' which is undefined without catch parameter.
Final Answer:
Missing error parameter in catch block -> Option D
Quick Check:
Catch needs error param = A [OK]
Hint: Always name error in catch(e) to use it inside [OK]
Common Mistakes:
Omitting error parameter in catch
Misplacing await outside try
Misusing console.error syntax
5. You want to run multiple async tasks in parallel and handle errors individually without stopping all tasks. Which pattern correctly handles errors for each async call using async/await?
hard
A. Wrap each await call inside its own try/catch block
B. Use Promise.all with a single try/catch around it
C. Use await inside a forEach loop with one try/catch outside
D. Ignore errors and rely on process.on('unhandledRejection')
Solution
Step 1: Understand parallel async calls and error handling
Promise.all stops on first rejection; one try/catch around it catches all or none.
Step 2: Individual error handling requires separate try/catch
Wrapping each await in its own try/catch lets errors be handled separately without stopping others.
Final Answer:
Wrap each await call inside its own try/catch block -> Option A
Quick Check:
Individual error handling = A [OK]
Hint: Try/catch each await separately for independent error handling [OK]
Common Mistakes:
Using Promise.all with one try/catch stops all on first error
Using forEach with await causes unexpected behavior
Ignoring errors leads to crashes or silent failures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.