Express 当一些axios请求失败时,如何成功处理这些请求
假设我有一个express服务器,它需要连接(发送数据)多个express服务器。像这样:Express 当一些axios请求失败时,如何成功处理这些请求,express,axios,es6-promise,Express,Axios,Es6 Promise,假设我有一个express服务器,它需要连接(发送数据)多个express服务器。像这样: app.post('/events',异步(req,res)=>{ const event=req.body; 事件。推送(事件) 试一试{ console.log(`event received${event.type}`) 等待axios.posthttp://localhost:4000/events",事件),; console.log('事件发送到4000') 等待axios.posthttp:
app.post('/events',异步(req,res)=>{
const event=req.body;
事件。推送(事件)
试一试{
console.log(`event received${event.type}`)
等待axios.posthttp://localhost:4000/events",事件),;
console.log('事件发送到4000')
等待axios.posthttp://localhost:4001/events",事件),;
console.log('事件发送到4001')
等待axios.posthttp://localhost:4002/events“,event);//正如@jfriend00建议您可以使用Promise.allsolited()
const promise1=axios.post('http://localhost:4000/events",活动)
const promise2=axios.post('http://localhost:4001/events",活动)
const promise3=axios.post('http://localhost:4002/events",活动)
const promise4=axios.post('http://localhost:4003/events",活动)
const promises=[promise1,promise2,promise3,promise4];
一切都解决了。
然后((results)=>results.forEach((result)=>console.log(result.status));
不要用try/catch
来捕捉拒绝你等待的承诺。我看不出有任何理由需要用await
一个接一个地排序。你可能可以去掉await
,把所有承诺放在一个数组中,然后使用promise.allSettled()
了解所有操作何时完成,然后检查哪些操作成功,哪些操作没有成功。