Guidelines for creating effects
Consistent effects helps ensure developers have access to same set of common objects when building components.
Returning response in effect and handling in dispatch
Example effect
This helps ensure dispatching code has access to server returned error messages.
*getUsersFromServer({ payload, cb }, { call }) {
let res;
let err;
try {
res = yield call(addLeadFollower, payload);
} catch (error) {
// extract the error response from the server
err = error
}
if(err){
// some api level error occurred. This can be handled in dispatch
return Promise.reject(err);
}
return res;
}
Example dispatch
// then while dispatching
dispatch({
type: "namespace/getUsersFromServer",
payload: payload,
})
.then((response) => {
// everything went well, do something with the response
})
.catch((err) => {
// some error occurred, do something with (err)
console.log("Server returned status code:", err);
});