Im playing around with some async calls (AXIOS
calls) involving promises. The problem with the code is the this.reloadData()
gets called before this.updateUsers()
and this.updateEmails()
executed and finished. What is the right way to wait for the two functions finish, and then call this.reloadData()
?
rest.putEmail
and rest.putUser
are both axios REST API calls.
mainFunc() {
Promise.all([this.updateUsers(), this.updateEmails()]).then(() => {
this.reloadData();
})
}
updateEmails() {
const {emails} = this.state;
if (emails.length > 0) {
emails.map(user => {
rest.putEmail(email.id, email.content)
.catch(err => {this.error(err)});
});
}
}
updateUsers() {
const {users} = this.state;
if (users.length > 0) {
users.map(user => {
rest.putUser(user.id, user.name)
.catch(err => {this.error(err)});
});
}
}