← All Exercises

Async/Await Error Handling

intermediatetypescript~25min

Background

Robust async error handling is critical for API calls, file operations, and any I/O that can fail unexpectedly.

Problem

Create an async function that fetches data from a URL, handles network and parse errors, and returns a typed result or error object.

Input

A URL string to fetch data from.

Output

A result object with either data or an error message, discriminated by a success flag.

Sample tests

Sample 1

Input: "https://api.example.com/data"
Expected: {success: true, data: {...}}

Your solution

Learning objective

Master async/await with typed error handling using discriminated unions.