gpt4 book ai didi

javascript - 等待不等待javascript

转载 作者:行者123 更新时间:2023-12-03 00:03:33 27 4
gpt4 key购买 nike

我有以下代码:

var arrayAxios = [];

const array = async() => {
await sleep(3000);
_.forEach(tasks, task => {
let res = _.includes("selected", task.value);
if (res) {
this.axios
.get(url, { params: { data1 } })
.then(response => {
arrayAxios.push(this.axios.post(url, { dataAAA }));
});
}
});
};

var promises = async() => {
const arrayAxios = await array();
await Promise.all(arrayAxios)
.then(response => {
resetAll_dataAAA(); // <--- my dataAAA in arrayAxios are reset before post in Promise.all!
})
.catch(error => {
console.log(error);
});
};

promises();

函数“resetAll_data()”在数据发送到 DDBB 之前执行。我找不到问题。

有什么建议吗?

非常感谢!

最佳答案

您正在寻找

async function array() {
await sleep(3000);
var arrayAxios = []; // declare the array locally
_.forEach(tasks, task => {
let res = _.includes("selected", task.value);
if (res) {
arrayAxios.push(this.axios
// ^^^^^^^^^^^^^^^ push to the array immediately, not asynchronously
.get(url, { params: { data1 } })
.then(response => {
return this.axios.post(url, { dataAAA }));
// ^^^^^^ chain the promises
})
);
}
});
return arrayAxios;
//^^^^^^ give the result to the caller
}

关于javascript - 等待不等待javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55077858/

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