gpt4 book ai didi

Javascript | For 循环之后执行

转载 作者:行者123 更新时间:2023-11-28 16:58:53 25 4
gpt4 key购买 nike

我想在 for 循环操作完成后调用一个函数,有什么方向吗?

这是我的代码:

       Function1: function () {
var $this = this;
$this.Action();
var l = $this.list.length;
for (var i = 0; i < l; i++) {
Function2(i)
}

function Function2(i){
axios.post('/api', $this.list[i])
.catch(function(error) {
console.log(error);
});
};
},

我想在 i > l || 时调用 $this.notAction()我==l

最佳答案

使用异步/等待

Function1: async function () {
var $this = this;
$this.Action();
var l = $this.list.length;
for (var i = 0; i < l; i++) {
await Function2(i)
}
$this.notAction();

async function Function2(i){
try {
await axios.post('/api', $this.list[i]);
} catch (err) {
console.log(err);
}
};
}

关于Javascript | For 循环之后执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com