Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Catch a Rejection

Asynchronous JavaScriptmedium

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

Implement an async function `safeDivide(a, b)` that returns `a / b`, but if `b` is `0` it throws and catches the error, returning `null`.

Sample tests

Input: safeDivide(10, 2)
Output: 5
Input: safeDivide(5, 0)
Output: null

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Throwing without a catch rejects the promise instead of returning null.

Learning resources

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

Wrapping the divide in try/catch converts the zero-divisor error into a null result.

Loading...
⌘/Ctrl + Enter

Run your code to see results.