Skip to main content

Practice ยท 2 of 3

Discriminated API Result

Write toApiResult(promise) โ€” awaits the promise and returns { ok: true, data } on success, { ok: false, status: 0, message: err.message } on failure. Write unwrap(result) โ€” returns data when ok, else THROWS an Error with the stored message. The pair must compose: unwrap(toApiResult(p)) returns the value or throws.

Difficulty: intermediate

Back to lesson: Practice: Mini Build: Typed API Model