axios 请求立即开始,不等待 axios.all

axios requests start right away not waiting for the axios.all

我的 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}`));