Part of my app uses decSubPassword to update state subPassword and then passes this state to function changePassword, which changes state in other context. I set sendData to run on button press. When I press button once, by analyzing logs I can see that subPassword hasn't changed which means, that decSubPassword function didn't update my state. However, when I press button second time (in short time) state instanly updates. That suggests to me, that there are some syncing problems and that application doesn't wait until decSubPassword is executed. I tried to make sendData function async and added await to decSubPassword as below, but it didn't solve my problem.
const sendData = async (id) => {
await decSubPassword({ id, secretKey });
changePassword(subPassword);
changeId(id);
changeUsername(username);
changeDomain(domain);
changeDescription(description);
console.log({ subPassword });
console.log(subPassword);
};
decSubPassword function
const decSubPassword = (dispatch) => async ({ id, secretKey }) => {
const response = await trackerApi.get("/api/password/" + id + "/");
const responsePassword = response.data.password;
const decPassword = decryptRecord(responsePassword, secretKey);
dispatch({ type: "dec_sub_password", payload: decPassword });
};