Javascript axios请求立即启动,无需等待axios

Javascript axios请求立即启动,无需等待axios,javascript,promise,es6-promise,Javascript,Promise,Es6 Promise,我的axios承诺没有如预期的那样起作用。我猜执行是从forEach循环内部开始的。我希望axios执行仅在批处理.commit之后开始 aMobileNumbers.forEach(function(iMobileNumber) { promises.push(axios.post('https://example.com', { 'app_id' : "XXXXXXX-7595", 'contents' : { "en":`${sTex

我的axios承诺没有如预期的那样起作用。我猜执行是从
forEach
循环内部开始的。我希望axios执行仅在批处理.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}`));  
aMobileNumbers.forEach(函数(iMobileNumber){
promises.push(axios.post)https://example.com', {
'应用程序id':“XXXXXXX-7595”,
'contents':{en:`${sText}`},
})
.then((响应)=>console.log(响应.data))
.catch((响应)=>console.log(响应))
);
})
log(`context.params.pushId}承诺:`,承诺);
返回batch.commit()。然后(()=>{
log(`writed`);
返回axios.all(promissions);//db.doc(`/MGAS/${context.params.pushId}`)。delete()
.then(()=>console.log(`Deleted the MQ`)))
.catch((error)=>console.log(`context.params.pushId}${error}`);

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

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}`));  
return batch.commit()。然后(()=>{
log(`writed`);
var promises=aMobileNumbers.map(函数(iMobileNumber){
返回axios.post('https://example.com“,{//console.log(response.data))
.catch((响应)=>console.log(响应));
})
log(`context.params.pushId}承诺:`,承诺);
返回axios.all(承诺);
})
.then(()=>db.doc(`/MGAS/${context.params.pushId}`)).delete())
.then(()=>console.log(`Deleted the MQ`)))
.catch((error)=>console.log(`context.params.pushId}${error}`);

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

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}`));  
return batch.commit()。然后(()=>{
log(`writed`);
var promises=aMobileNumbers.map(函数(iMobileNumber){
返回axios.post('https://example.com“,{//console.log(response.data))
.catch((响应)=>console.log(响应));
})
log(`context.params.pushId}承诺:`,承诺);
返回axios.all(承诺);
})
.then(()=>db.doc(`/MGAS/${context.params.pushId}`)).delete())
.then(()=>console.log(`Deleted the MQ`)))
.catch((error)=>console.log(`context.params.pushId}${error}`);

我并不特别希望在
提交之后调用请求,但我只想知道函数是否会等待所有axios请求完成?在原始代码和答案中的代码中,所有请求都将在调用delete方法之前完成。区别在于,在问题代码中,ajax请求将立即启动,在应答代码中,ajax请求将在批处理提交后启动。@啊。那么为什么您会问“我希望axios的执行仅在批处理提交后启动?”?顺便说一句,我建议在这里使用
map
而不是
forEach
。编辑代码以引入
map
而不是
forEach
。我最初没有更改它,以使答案代码与原始问题代码相似。我并不特别希望在
提交后调用请求,但我只是ant想知道函数是否会等待所有axios请求完成?在原始代码和答案中的代码中,所有请求都将在调用delete方法之前完成。区别在于,在问题代码中,ajax请求将立即启动,在答案代码中,ajax请求将在批处理之后启动提交。@啊。那你为什么问“我希望axios的执行只在批处理后开始。提交”
?顺便说一句,我建议在这里使用
map
而不是
forEach
。编辑代码以介绍
map
而不是
forEach
。我最初没有更改它,因此答案代码与原始问题代码相似。