Skip to content

Instantly share code, notes, and snippets.

@davidicus
Created January 10, 2018 21:31
Show Gist options
  • Save davidicus/235c8fba63c2c3c8f987ab3dee7003dd to your computer and use it in GitHub Desktop.
Save davidicus/235c8fba63c2c3c8f987ab3dee7003dd to your computer and use it in GitHub Desktop.
Fetch api and make error fail loudly
fetch(path.join(__dirname, `/api/structures/overview`), {
method: `GET`,
credentials: 'include'
})
.then(response => {
const json = response.json();
if (response.status >= 200 && response.status < 300) {
return json;
} else {
return json.then(Promise.reject.bind(Promise));
}
})
.then(responseJson => {
dispatch(getStructuresSuccess(responseJson));
return responseJson;
})
.catch((error) => {
dispatch(getStructuresFailure(error));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment