Bird
0
0

Which of the following is the correct syntax to catch errors in an async function using async/await?

easy📝 Syntax Q12 of 15
Node.js - Error Handling Patterns
Which of the following is the correct syntax to catch errors in an async function using async/await?
Aasync function fetchData() { await fetch(); } catch (e) { console.error(e); }
Basync function fetchData() { (await fetch()).catch(e => console.error(e)); }
Casync function fetchData() { try { await fetch(); } catch (e) { console.error(e); } }
Dasync function fetchData() { try await fetch(); catch (e) { console.error(e); } }
Step-by-Step Solution
Solution:
  1. Step 1: Identify proper try/catch block usage

    Try/catch must wrap the await expression inside the async function body.
  2. Step 2: Check syntax correctness

    async function fetchData() { try { await fetch(); } catch (e) { console.error(e); } } correctly places try before await and catch after the block.
  3. Final Answer:

    async function fetchData() { try { await fetch(); } catch (e) { console.error(e); } } -> Option C
  4. Quick Check:

    Correct try/catch syntax = C [OK]
Quick Trick: Wrap await inside try block, catch errors after [OK]
Common Mistakes:
  • Placing catch outside function body
  • Using .catch() on await directly
  • Using try without braces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes