I need to wait for all of the axios calls in a function called by a forEach loop to run before I call another function to reload data that should be updated by the axios calls.
function1() {
let arr = [1, 2, 3, 4, 5];
arr.forEach((num) => {
function2(num);
});
//Wait until each call in the forEach loop above
//is done before running the function3() function.
function3();
}
function2(number) {
axios.post('/internal-url/action/' + number)
.then((response) => {
return response;
});
}
function3() {
console.log('reloading data...');
/* DB call to reload data */
console.log('data is reloaded');
}
The problem here is that the data reloads before the axios calls are complete. I know the axios calls worked because I can see they updated in the database, but I need for function1() to wait for all of the axios calls in function2() to finish before running the reload in function3().
I've tried to make function1() and function2() async/await functions, both at the same time and then individually, but that did not work. How would I go about doing this specifically?