Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Async try/catch Fallback

Asynchronous JavaScriptmedium

Course · Section 16: Asynchronous JavaScript: Promises, Async/Await, and AJAX · Lecture 275: Error Handling With try...catch

Implement an async function `fetchSafe(shouldFail, value, fallback)` that throws when `shouldFail` is true, catches it, and returns `fallback`; otherwise returns `value`.

Sample tests

Input: fetchSafe(false, 10, 0)
Output: 10
Input: fetchSafe(true, 10, -1)
Output: -1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • An uncaught throw rejects the returned promise instead of yielding the fallback.
Visualize this concept: Async/Await →
Approach & explanation (try first)

try/catch inside an async function converts a failure into a returned fallback value.

Loading...
⌘/Ctrl + Enter

Run your code to see results.