How do I write these functions so that the dispatch finalizes before the if statements start? Currently, the if statements start console.log()'ing before the dispatch can even save the data.
const doStuff = () => {
dispatch(getStuff()); // WAIT FOR ME TO COMPLETE BEFORE MOVING ON!!!
if (stuff) { //stuff is retreived from global state
console.log("stuff available") //I WAITED FOR THE DISPATCH!
}
if (!stuff) { //stuff is retreived from global state
console.log("no stuff") //I WAITED FOR THE DISPATCH!
}
};
export const getStuff = () => { \\WAIT FOR ME TO SAVE STUFF!!!
return (dispatch) => {
axios.get(`/stuff`).then((response) => {
dispatch({
type: SAVE_STUFF_TO_STATE, //stuff is saved to global state
payload: response.data, //stuff
});
})
} catch (err) {
console.log("stuff issue")
}
};
};