I am using redux-thunk to make action calls and reducers to return back states. My actions are essentially axios API requests to the back end. For one particular action, I need a set events dispatched in the exact order as they are shown in the code:
- Check if
tokenvaluepassed by the user is valid (which has its own axios api request to the tokencollection). If 1. fails, jump to the catch block. - If token is indeed valid, register the user using axios post. if 2. fails, jump to catch block
- If user was registered successfully, set the token to the user (thus only one unique token per user). If 3. fails, jump to catch block.
In order to implement them sequentially in the order above, I put them in try-catch blocks. Turns out my understanding of how dispatch works is wrong - if a dispatch fails with an error, it still executes subsequent dispatches. Any suggestions on how I can solve this? :
export const register = ({name,email,password,tokenval}) => async(dispatch) =>{
try{
await dispatch(checkTokenValidity(tokenval)); // if this fails, jump to catch block
const res = await axios.post("/api/users", body, config); //if this fails jump to catch
await dispatch(setUserToToken({ tokenval, userID: res.data.userID })); //if this fails jump to catch
dispatch({
type: REGISTER_SUCCESS,
payload: res.data,
});
}catch(err){
dispatch({
type: REGISTER_FAIL,
});
}
};