DEV Community

Discussion on: Promise Chains are Kinda Awesome

Collapse
djorg83 profile image
Daniel Jorgensen

What about error handling?

Collapse
bennypowers profile image
Benny Powers 🇮🇱🇨🇦 Author

Excellent question!

If you want to use Promise as your container, as we've done in the examples, you can forward the exceptions from the fetchers in a local catch block:

const rethrow = message => err => { 
  throw new Error(`${message} ${error}`)
}

const fetchGuests = () => 
  fetch('/friends')
    .catch(rethrow('Could not fetch guests');

etc. season to taste, then handle them in the top level flow's catch block.

If you want to go buck wild though, you could toss in a few natural transformations. That however, will have to wait for another post :D