Skip to content

Instantly share code, notes, and snippets.

@lilactown
Last active September 19, 2017 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilactown/aeb226c7d086e39c54f47386d9f0c636 to your computer and use it in GitHub Desktop.
Save lilactown/aeb226c7d086e39c54f47386d9f0c636 to your computer and use it in GitHub Desktop.
fetch('/endpoint')
.then(res => {
if (res.ok) {
return res.json();
}
throw new Error(res.statusText);
})
// handle response here
.then(({ status, payload }) => {
switch (status) {
// ...handle various responses that the service may have sent and return a corresponding value
case [OK]:
return nextAction(payload);
case [SOME_OTHER_STATUS]:
return aDifferentAction(payload);
default:
throw new Error("unknown status");
}
})
.then(action => dispatch(action))
.catch(err => {
// handle errors and dispatch appropriate error messaging
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment