如何等到Javascript forEach循环完成后再继续下一步

如何等到Javascript forEach循环完成后再继续下一步,javascript,asynchronous,foreach,async-await,axios,Javascript,Asynchronous,Foreach,Async Await,Axios,我需要等待forEach循环调用的函数中的所有axios调用运行,然后再调用另一个函数来重新加载应由axios调用更新的数据 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 runnin

我需要等待forEach循环调用的函数中的所有axios调用运行,然后再调用另一个函数来重新加载应由axios调用更新的数据

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');
}
这里的问题是在axios调用完成之前重新加载数据。我知道axios调用工作正常,因为我可以在数据库中看到它们已更新,但我需要
function1()
等待
function2()
中的所有axios调用完成,然后在
function3()
中运行重新加载


我尝试过同时和分别创建
function1()
function2()
async/await
函数,但都不起作用。我该如何具体做到这一点呢?

最好的解决方案是使用
Promise.all
,这样所有请求都可以并行进行。这看起来像这样

function1() {
    let arr = [1, 2, 3, 4, 5];
    Promise.all(arr.map((num) => function2(num))).then(() => {
        function3();
    });
}

这将等到
function2
返回的所有承诺都得到解决后再调用
function3
最好的解决方案是使用
Promise。所有
都可以并行执行所有请求。这看起来像这样

function1() {
    let arr = [1, 2, 3, 4, 5];
    Promise.all(arr.map((num) => function2(num))).then(() => {
        function3();
    });
}

这将等到
function2
返回的所有承诺都得到解决后,再调用
function3

创建一个承诺数组,然后使用来等待它们的解决/拒绝


创建一系列承诺,然后使用等待解决/拒绝

也许是这样:

const arr = [1, 2, 3, 4, 5];
let index = 0;
const answers = [];
(function loop() {
    const value = arr[index];
    function2(value).then(res => {
        answers.push(res);
    });
    index++;
    if (index < arr.length) {
        loop();
    }
})();
function3();
console.log(answers);
const arr=[1,2,3,4,5];
设指数=0;
常量answers=[];
(函数循环(){
常量值=arr[索引];
函数2(值)。然后(res=>{
答案:推(res);
});
索引++;
if(索引
可能是这样的:

const arr = [1, 2, 3, 4, 5];
let index = 0;
const answers = [];
(function loop() {
    const value = arr[index];
    function2(value).then(res => {
        answers.push(res);
    });
    index++;
    if (index < arr.length) {
        loop();
    }
})();
function3();
console.log(answers);
const arr=[1,2,3,4,5];
设指数=0;
常量answers=[];
(函数循环(){
常量值=arr[索引];
函数2(值)。然后(res=>{
答案:推(res);
});
索引++;
if(索引
forEach
不是异步的。您想要实现的是默认行为,我对Axios并不特别了解,但在JavaScript中执行此类操作的通常方式是在AJAX调用完成时调用“回调”。例如,您可能有一个计数器,该计数器由
foreach
循环递增(跟踪未完成请求的数量),然后在调用的回调中,从该计数器中减去1并测试为零。当它达到零时,则调用
函数3
forEach
不是异步的。您想要实现的是默认行为,我对Axios并不特别了解,但在JavaScript中执行此类操作的通常方式是在AJAX调用完成时调用“回调”。例如,您可能有一个计数器,该计数器由
foreach
循环递增(跟踪未完成请求的数量),然后在调用的回调中,从该计数器中减去1并测试为零。当它达到零时,你调用你的
函数3
…这就行了。这是非常直接的,并且将允许我做一些研究来真正理解为什么/如何工作。@aCarella:您可能想使用
const response=wait Promise.all(promises)响应
数据(数组)执行操作。我没有在回答中加上这个,因为不清楚你是否在用它做任何事情。这很有效。这是非常直接的,并且将允许我做一些研究来真正理解为什么/如何工作。@aCarella:您可能想使用
const response=wait Promise.all(promises)响应
数据(数组)执行操作。我没有在回答中添加这一点,因为不清楚你是否在用它做任何事情。