Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Await with Default

Asynchronous JavaScriptmedium

Course · Section 16: Asynchronous JavaScript: Promises, Async/Await, and AJAX · Lecture 263: Promises and the Fetch API

Implement an async function `fetchOrDefault(value, fallback)` returning `value` when it is not null or undefined, otherwise `fallback`.

Sample tests

Input: fetchOrDefault(null, 5)
Output: 5
Input: fetchOrDefault(3, 5)
Output: 3

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Using || would replace valid falsy values like 0.

Learning resources

  • MDN: async function
Visualize this concept: Async/Await →
Approach & explanation (try first)

?? returns the value unless it is nullish, otherwise the fallback, inside an async function.

Loading...
⌘/Ctrl + Enter

Run your code to see results.