gpt4 book ai didi

javascript - axios 请求立即开始,无需等待 axios.all

转载 作者:行者123 更新时间:2023-12-01 00:56:15 32 4
gpt4 key购买 nike

我的 axios promise 没有按预期工作。我猜执行是在 forEach 循环内开始的。我希望 axios 执行仅在 batch.commit

之后开始
aMobileNumbers.forEach(function(iMobileNumber) {
promises.push(axios.post('https://example.com', {
'app_id' : "XXXXXXX-7595",
'contents' : { "en":`${sText}` },
})
.then((response) => console.log(response.data))
.catch((response) => console.log(response))
);
})

console.log(`#${context.params.pushId} Promises: `, promises);

return batch.commit().then(() => {
console.log(`wrote`);
return axios.all(promises); //<--- doesnot execute here
})
.then(() => db.doc(`/MGAS/${context.params.pushId}`).delete())
.then(() => console.log(`Deleted the MQ`))
.catch((error) => console.log(`#${context.params.pushId} ${error}`));

最佳答案

调用 axios post 方法确实会启动请求。如果您想在提交后启动请求,您应该将代码放在 then 回调中:

return batch.commit().then(() => {
console.log(`wrote`);

var promises = aMobileNumbers.map(function(iMobileNumber) {
return axios.post('https://example.com', { // <--- does execute here
'app_id' : "XXXXXXX-7595",
'contents' : { "en":`${sText}` },
})
.then((response) => console.log(response.data))
.catch((response) => console.log(response));
})

console.log(`#${context.params.pushId} Promises: `, promises);

return axios.all(promises);
})
.then(() => db.doc(`/MGAS/${context.params.pushId}`).delete())
.then(() => console.log(`Deleted the MQ`))
.catch((error) => console.log(`#${context.params.pushId} ${error}`));

关于javascript - axios 请求立即开始,无需等待 axios.all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56557743/

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