如何同时发布多个Axios请求? [英] How to post multiple Axios requests at the same time?

查看:552
本文介绍了如何同时发布多个Axios请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我有一个网页,其中正在制作一长串Axios POST调用.现在,请求似乎是并行发送的(JavaScript会在接收到结果之前继续发送下一个请求).

At this moment I have a webpage in which a long list of Axios POST calls are being made. Now, the requests seem to be sent in parallel (JavaScript continues sending the next request before the result is received).

但是,结果似乎是一个接一个地返回,而不是同时返回.假设一次对PHP脚本的POST调用需要4秒钟,而我需要进行10次调用.目前,每次通话需要4秒钟,总计40秒钟.我希望找到一种解决方案,并在大约同一时间(〜4秒)而不是大约40秒接收所有结果.

However, the results seem to be returned one by one, not simultaneously. Let's say one POST call to the PHP script takes 4 seconds and I need to make 10 calls. It would currently take 4 seconds per call, which would be 40 seconds in total. I hope to find a solution to both and receive all results at approximately the same time (~4 seconds) instead of ~40 seconds.

现在,我已经了解了有关线程的信息,以及使用Workers在NodeJS中进行多线程处理的知识.我已经读过JavaScript本身只是单线程的,因此它本身可能不允许这样做.

Now I've read about threads, multithreading in NodeJS using Workers. I've read that JavaScript itself is only single-threaded, so it may not allow this by itself.

但是我不确定从这儿去哪里.我所拥有的只是一些想法.我不确定我是否朝着正确的方向前进,如果不确定,我不确定如何在NodeJS中使用Workers并将其应用到我的代码中.我应该走哪条路?任何指导将不胜感激!

But I'm not sure where to go from here. All I have are some ideas. I'm not sure whether or not I'm heading into the right direction and if I am, I am not sure how to use Workers in NodeJS and apply it in my code. Which road should I take? Any guidance would be highly appreciated!

这是一小段示例代码:

for( var i = 0;  i < 10;  i++ )
{
    window.axios.post(`/my-url`, {
        myVar: 'myValue'
    })
    .then((response) => {
        // Takes 4 seconds, 4 more seconds, 4 more seconds, etc
        // Ideally: Takes 4 seconds, returns in the same ~4 seconds, returns in the same ~4 seconds, etc
        console.log( 'Succeeded!' );
    })
    .catch((error) => {
        console.log( 'Error' );
    });

    // Takes < 1 second, < 1 more second, < 1 more second, etc
    console.log( 'Request sent!' );
}

推荐答案

有三种情况可以帮助您实现目标.

There are three cases via you can achieve your goal.

1)对于与Axios的同时请求,可以使用Axios.all()

1) For simultaneous requests with Axios, you can use Axios.all()

axios.all([
  axios..post(`/my-url`, {
    myVar: 'myValue'
  }), 
  axios.post(`/my-url2`, {
    myVar: 'myValue'
  })
])
.then(axios.spread((data1, data2) => {
  // output of req.
  console.log('data1', data1, 'data2', data2)
}));

2)您可以使用Promise.allSettled(). Promise.allSettled()方法返回一个承诺,该承诺将在所有给定的承诺均已解决或被拒绝后解决,

2) you can use Promise.allSettled(). The Promise.allSettled() method returns a promise that resolves after all of the given promises have either resolved or rejected,

3)您可以尝试使用Promise.all(),但是它的缺点是,如果任何 1 请求失败,那么它将对所有人失败,并把o/p作为错误(或在catch块中给出) )

3) You can try to use Promise.all() but it has the drawback that if any 1 req failed then it will fail for all and give o/p as an error(or in catch block)

但最好的情况是第一个.

but the best case is the first one.

这篇关于如何同时发布多个Axios请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆