In ReactJS + Redux project, I have a method where it makes an API request. If successful, I would like to dispatch another action creator and wait until it finishes. Then when it completes, move onto the next step.
Currently, the following code does the dispatch where it makes another API call but even before the state gets updated via dispatch, it immediately executes window.location.href ='http://localhost:3005/#/Home'
and then the dispatch gets completed afterwards.
So how can I wait until the dispatch(actions.updateUserInfo(userInfo.username))
gets completed before executing 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 => {
//Would like to finish this dispatch completely before start executing window.location.href ='http://localhost:3005/#/Home'
dispatch(actions.updateUserInfo(userInfo.username))
window.location.href ='http://localhost:3005/#/Home'
})
.catch((error) => {
console.log("Error: ", error)
})
}
Thank you in advance