Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ax-throttle

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ax-throttle

Library which allows to throttle axios requests

  • 1.0.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-85.71%
Maintainers
0
Weekly downloads
 
Created
Source

axios-throttle

Library which allows to throttle axios library request

Installation

npm install axios-throttle

Usage

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 urls = [
  '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 < urls.length; index++) {
  options.url = urls[index];
  promises.push(axiosThrottle.getRequestPromise(options, index));
}

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

Run example

npm i
npm run example

API

init

Initializes library
@param {any} axiosArg axios - object
@param {number} delayBetweenRequests - delay between requests in miliseconds (If you want to send 5 requests per second you need to set value of this parameter to 200)

getRequestPromise

Returns request's promise
@param {any} options - axios options
@param {number} index - index from urls array

Keywords

FAQs

Package last updated on 03 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc