Axios请求在Promise.all之前触发
因此我的问题指出,Axios请求在Promise.all之前发布一些信息
所以这是代码
newMessages = newMessages.map(message =>
axios.post(URL, message, {
auth: { username, password },
})
);
// Axios requests finishes before I call Promise.all, and if I call Promise.all the requests sends second time
await Promise.all(newMessages);
我已经尝试了其他一些变体,但所有的变体都相同
let sendMessages = [];
for (let i = 0; i < newMessages.length; i++) {
sendMessages.push(
axios.post(
URL,
newMessages[i],
{
auth: { username, password },
}
)
);
}
// Axios requests finishes before I call Promise.all, and if I call Promise.all the requests sends second time
await Promise.all(sendMessages);
回答如下:newMessages = newMessages.map(message =>
axios.post(URL, message, {
auth: { username, password },
}).then(res => res)
);
await Promise.all(newMessages);
Axios请求在Promise.all之前触发
因此我的问题指出,Axios请求在Promise.all之前发布一些信息
所以这是代码
newMessages = newMessages.map(message =>
axios.post(URL, message, {
auth: { username, password },
})
);
// Axios requests finishes before I call Promise.all, and if I call Promise.all the requests sends second time
await Promise.all(newMessages);
我已经尝试了其他一些变体,但所有的变体都相同
let sendMessages = [];
for (let i = 0; i < newMessages.length; i++) {
sendMessages.push(
axios.post(
URL,
newMessages[i],
{
auth: { username, password },
}
)
);
}
// Axios requests finishes before I call Promise.all, and if I call Promise.all the requests sends second time
await Promise.all(sendMessages);
回答如下:newMessages = newMessages.map(message =>
axios.post(URL, message, {
auth: { username, password },
}).then(res => res)
);
await Promise.all(newMessages);