axios.all,如何配置axios等待时间来缓解挂机?

Posted

技术标签:

【中文标题】axios.all,如何配置axios等待时间来缓解挂机?【英文标题】:Axios.all, how to configure axios wait time to mitigate hung up? 【发布时间】:2018-09-29 07:14:10 【问题描述】:

我的应用程序使用内部 Web 服务来获取数据,我有一个创建大约 500 个请求的作业,这些请求会异步触发以完成获取操作。 我使用 Axios,通过创建一个 axios 承诺数组,然后使用 Axios.all() 解决它们;

它工作正常,直到大约 200 个请求,但发布后我得到套接字挂起,但是在服务器端我看到请求正在处理中。

如何配置 axios 来设置自定义超时,或者将我的 Promise 数组拼接然后作为多个批次运行它们是一个更好的主意?

源代码

let getAxiosPromiseArray = (urlList) => 
    var axiosArrayofPromise = [];

    return new Promise ( (resolve, reject) => 


        try 
            urlList.forEach ( (URL) => 
                axiosArrayofPromise.push(axios.get(URL));
            );
            resolve(axiosArrayofPromise);

        
        catch (err) 
            reject("There is a problem getting Axios array of promises " + err);
        

    )


async function processAxiosPromises (PromiseArray) 
    try 
        var results = []
        results =  await axios.all(PromiseArray);
        return results;
    
    catch(err) 
        throw("There was a problem resolving promises array (Axios) " + err);
    





getallID().then ( (urlList) => 
    return getAxiosPromiseArray(urlList);
).then( (AxiosPromises) => 
    return processAxiosPromises(AxiosPromises);
).then ((resultData) => 
    console.log(resultData);
);

错误

There was a problem resolving promises array (Axios) Error: socket hang up

【问题讨论】:

【参考方案1】:

首先,getAxiosPromiseArray()processAxiosPromises() 这对函数需要修复。

您的new Promise() 构造是不必要的。您可以简单地 return Promise.all(arrayofPromise)(或 axios.all(...) 如果您必须)并取消其他功能。

将剩余的函数重命名为有意义的东西,你最终会得到例如:

let getData = (urlList) => 
    return Promise.all(urlList.map(URL => axios.get(URL)))
    .catch(error => 
        error.message = "There is a problem getting Axios array of promises " + error.message; // augment the error message ...
        throw error; // ... and re-throw the errror.
    );
;

并调用如下:

getallID().then(getData)
.then(resultData => 
    console.log(resultData);
).catch(error => 
    console.error(error);
);

这将使您站稳脚跟,但就其本身而言,不太可能解决并发问题(如果是这样的话),最简单的方法是使用 Bluebird's Promise.map 和 concurrency 选项。

调用方代码可以保持不变,只需更改getData(),如下:

let getData = (urlList) => 
    let concurrency = 10; // play with this value to find a reliable concurrency limit
    return Promise.map(urlList, URL => axios.get(URL), 'concurrency': concurrency)
    .catch(error => 
        error.message = "There is a problem getting Axios array of promises " + error.message;
        throw error;
    );
;
// where `Promise` is Bluebird.

【讨论】:

【参考方案2】:
const axios = require('axios');
const axiosThrottle = require('axios-throttle'); 
//pass axios object and value of the delay between requests in ms
axiosThrottle.init(axios,200)
const options = 
  method: 'GET',
;
const urlList = [
  'https://jsonplaceholder.typicode.com/todos/1',
  'https://jsonplaceholder.typicode.com/todos/2',
  'https://jsonplaceholder.typicode.com/todos/3',
  'https://jsonplaceholder.typicode.com/todos/4',
  'https://jsonplaceholder.typicode.com/todos/5',
  'https://jsonplaceholder.typicode.com/todos/6',
  'https://jsonplaceholder.typicode.com/todos/7',
  'https://jsonplaceholder.typicode.com/todos/8',
  'https://jsonplaceholder.typicode.com/todos/9',
  'https://jsonplaceholder.typicode.com/todos/10'
];
const promises = [];
const responseInterceptor = response => 
  console.log(response.data);
  return response;
;

//add interceptor to work with each response seperately when it is resolved
axios.interceptors.response.use(responseInterceptor, error => 
  return Promise.reject(error);
);

for (let index = 0; index < urlList.length; index++) 
  options.url = urlList[index];
  promises.push(axiosThrottle.getRequestPromise(options, index));


//run when all promises are resolved
axios.all(promises).then(responses => 
  console.log(responses.length);
);

https://github.com/arekgotfryd/axios-throttle

【讨论】:

以上是关于axios.all,如何配置axios等待时间来缓解挂机?的主要内容,如果未能解决你的问题,请参考以下文章

Axios/Vue - 防止 axios.all() 继续执行

axios如何一次性发送多个网络请求?

根据选项将 axios.get URL 传递给 axios.all

Axios.all 在 Nestjs 中可用吗?

axios.all 的动态使用

无法从 getServerSideProps 返回 axios.all 数据