запросы axios начинаются сразу, не ожидая axios.all - PullRequest
0 голосов
/ 12 июня 2019

Мое обещание Axios не работает должным образом.Я думаю, что выполнение начинается внутри цикла 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}`));  

1 Ответ

2 голосов
/ 12 июня 2019

Вызов метода axios post действительно запустит запрос. Если вы хотите начать запросы после фиксации, вы должны поместить свой код в then callback:

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