Last active
September 19, 2017 17:05
-
-
Save lilactown/aeb226c7d086e39c54f47386d9f0c636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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