In a ReactJS + Redux project, I have a method in which it executes an API request. If successful, I would like to send another creator of the action and wait until it ends. Then, when it is complete, continue to the next step.
Currently, the following code sends to where it calls another API call, but even before the state is updated through dispatch, it immediately executes window.location.href ='http://localhost:3005/#/Home', after which the sending will be completed later.
So, how can I wait for the completion of dispatch(actions.updateUserInfo(userInfo.username))the next line of code window.location.href ='http://localhost:3005/#/Home'?
Here is the action creator:
loggingIn(userInfo) {
var userInfoBody = {
'username': `${userInfo.username}`,
'password': `${userInfo.password}`
}
var configuration = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userInfoBody)
}
return function(dispatch) {
fetch('https://backendserver.com:8080/creds', configuration)
.then(response => response.json())
.then(response => {
dispatch(actions.updateUserInfo(userInfo.username))
window.location.href ='http://localhost:3005/#/Home'
})
.catch((error) => {
console.log("Error: ", error)
})
}
Thank you in advance
user3259472