Detecting Fetch Typeerrors In Promise Chain
Currently, I'm using fetch with redux-thunk to read code from an API - my code reads like this: export function getUsers() { return (dispatch) => { // I have some helper
Solution 1:
Don't use .catch()
but install the error handler directly on the fetch promise, as the second - onreject - argument to .then()
:
returnfetch(`/users`)
.then(([resp, json]) => {
if (resp.status === 200) {
dispatch(getUsersSuccess(json));
} else {
dispatch(getUsersFail(json));
}
}, (err) => {
// network errordispatch(getUsersFail(err));
});
Check out the difference between .then(…).catch(…)
and .then(…, …)
for details.
Btw, I'd recommend to write
returnfetch(`/users`)
.then(([resp, json]) => resp.status === 200 ? getUsersSuccess(json) : getUsersFail(json)
, getUsersFail)
.then(dispatch);
Post a Comment for "Detecting Fetch Typeerrors In Promise Chain"