Handling Rejections

Rejection
πŸ‘¨β€πŸ’Ό Our API sometimes fails, and we need to handle those failures gracefully. Rejected Promises should be caught so we can return a friendly error state.
🐨 Open
index.ts
and:
  1. Keep fetchUser(id) as provided:
    • id === '1' resolves to Alice (id: '1', name: 'Alice', email: 'alice@example.com')
    • any other id rejects with Error('User not found')
  2. Update getUserProfile(id) to handle rejections with .catch()
  3. Return shapes:
    • success: { status: 'success', user }
    • error: { status: 'error', message } where message is error.message when the rejection is an Error, otherwise 'Unknown error'
  4. Export both fetchUser and getUserProfile
Completion checks:
  • await getUserProfile('1') β†’ { status: 'success', user: { id: '1', ... } }
  • await getUserProfile('missing') β†’ { status: 'error', message: 'User not found' }
  • await fetchUser('missing') still rejects (do not swallow that rejection inside fetchUser itself)

Please set the playground first

Loading "Handling Rejections"
Loading "Handling Rejections"
Login to get access to the exclusive discord channel.
Loading Discord Posts