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

查看:53
本文介绍了如何同时发布多个 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()

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

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

  • 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,

    你可以尝试使用 Promise.all() 但它有一个缺点,如果任何 1 请求失败,那么它会失败并给出 o/p 作为错误(或在 catch 块中)

    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天全站免登陆