New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

throttled-queue

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

throttled-queue - npm Package Compare versions

Comparing version 1.0.7 to 2.0.0

.eslintignore

32

package.json
{
"name": "throttled-queue",
"version": "1.0.7",
"version": "2.0.0",
"description": "Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests.",
"main": "throttled-queue.js",
"main": "dist/throttledQueue.js",
"directories": {
"lib": "src",
"test": "test"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha --recursive --timeout 200000"
"prepublishOnly": "tsc",
"test": "mocha"
},
"mocha": {
"spec": "test/throttledQueue.test.ts",
"extension": [
"ts"
],
"require": [
"ts-node/register"
],
"timeout": 200000
},
"repository": {

@@ -30,8 +42,12 @@ "type": "git",

"devDependencies": {
"gulp": "^3.9.1",
"gulp-concat": "^2.6.0",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^1.5.2",
"mocha": "^2.4.5"
"@types/mocha": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"eslint": "^8.6.0",
"eslint-config-airbnb-typescript": "^16.1.0",
"eslint-plugin-import": "^2.25.4",
"mocha": "^9.1.3",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
}
}

@@ -10,23 +10,27 @@ # throttled-queue

## Installation
Can be used in a Node.js environment, or directly in the browser.
### Node.js
`npm install throttled-queue`
### Browser
`<script src="throttled-queue.min.js"></script>`
```shell
npm install throttled-queue
```
It can be used in a Node.js environment, or directly in the browser.
## Usage
1) If in node.js, `require` the factory function:
```js
var throttledQueue = require('throttled-queue');
1) `require` or `import` the factory function:
```javascript
const throttledQueue = require('throttled-queue');
```
Else, include it in a script tag in your browser and `throttledQueue` will be globally available.
```javascript
import throttledQueue from 'throttled-queue';
```
2) Create an instance of a throttled queue by specifying the maximum number of requests as the first parameter,
and the interval in milliseconds as the second:
```js
var throttle = throttledQueue(5, 1000); // at most 5 requests per second.
```javascript
const throttle = throttledQueue(5, 1000); // at most 5 requests per second.
```
3) Use the `throttle` instance as a function to enqueue actions:
```js
throttle(function() {
```javascript
throttle(() => {
// perform some type of activity in here.

@@ -36,14 +40,21 @@ });

The `throttle` function will also return a promise with the result of your operation:
```javascript
const result = await throttle(() => {
return Promise.resolve('hello!');
});
// result now equals "hello"
```
## Quick Examples
### Basic
Rapidly assigning network calls to be run, but they will be limited to 1 request per second.
```js
var throttledQueue = require('throttled-queue');
var throttle = throttledQueue(1, 1000); // at most make 1 request every second.
```javascript
const throttledQueue = require('throttled-queue');
const throttle = throttledQueue(1, 1000); // at most make 1 request every second.
for (var x = 0; x < 100; x++) {
throttle(function() {
for (let x = 0; x < 100; x++) {
throttle(() => {
// make a network request.
fetch('https://api.github.com/search/users?q=shaunpersad').then(console.log);
return fetch('https://api.github.com/search/users?q=shaunpersad');
});

@@ -55,18 +66,16 @@ }

and be subject to the same rate limits.
```js
var throttledQueue = require('throttled-queue');
var throttle = throttledQueue(1, 60 * 1000); // at most make 1 request every minute.
```javascript
const throttledQueue = require('throttled-queue');
const throttle = throttledQueue(1, 60 * 1000); // at most make 1 request every minute.
for (var x = 0; x < 50; x++) {
throttle(function() {
for (let x = 0; x < 50; x++) {
throttle(() => {
// make a network request.
fetch('https://api.github.com/search/users?q=shaunpersad').then(console.log);
return fetch('https://api.github.com/search/users?q=shaunpersad');
});
}
for (var y = 0; y < 50; y++) {
throttle(function() {
for (let y = 0; y < 50; y++) {
throttle(() => {
// make another type of network request.
fetch('https://api.github.com/search/repositories?q=throttled-queue+user:shaunpersad').then(console.log);
return fetch('https://api.github.com/search/repositories?q=throttled-queue+user:shaunpersad');
});

@@ -77,11 +86,10 @@ }

By specifying a number higher than 1 as the first parameter, you can dequeue multiple actions within the given interval:
```js
var throttledQueue = require('throttled-queue');
var throttle = throttledQueue(10, 1000); // at most make 10 requests every second.
```javascript
const throttledQueue = require('throttled-queue');
const throttle = throttledQueue(10, 1000); // at most make 10 requests every second.
for (var x = 0; x < 100; x++) {
throttle(function() {
for (let x = 0; x < 100; x++) {
throttle(() => {
// This will fire at most 10 a second, as rapidly as possible.
fetch('https://api.github.com/search/users?q=shaunpersad').then(console.log);
return fetch('https://api.github.com/search/users?q=shaunpersad');
});

@@ -92,23 +100,42 @@ }

You can space out your actions by specifying `true` as the third (optional) parameter:
```js
var throttledQueue = require('throttled-queue');
var throttle = throttledQueue(10, 1000, true); // at most make 10 requests every second, but evenly spaced.
```javascript
const throttledQueue = require('throttled-queue');
const throttle = throttledQueue(10, 1000, true); // at most make 10 requests every second, but evenly spaced.
for (var x = 0; x < 100; x++) {
throttle(function() {
throttle(() => {
// This will fire at most 10 requests a second, spacing them out instead of in a burst.
fetch('https://api.github.com/search/users?q=shaunpersad').then(console.log);
return fetch('https://api.github.com/search/users?q=shaunpersad');
});
}
```
### Promises
Starting in version `2.0.0`, you can wait for the results of your operation:
```javascript
const throttledQueue = require('throttled-queue');
const throttle = throttledQueue(10, 1000, true); // at most make 10 requests every second, but evenly spaced.
## Tests
Note: The tests take a few minutes to run. Watch the console to see how closely the actual rate limit gets to the maximum.
### Node.js
Run `npm test`.
### Browser
Open `test/index.html` in your browser.
const usernames = ['shaunpersad', 'forward-motion'];
const profiles = await Promise.all(usernames.map((username) => {
return throttle(() => {
return fetch(`https://api.github.com/search/users?q=${username}`);
});
}));
const justMe = await throttle(() => fetch('https://api.github.com/search/users?q=shaunpersad'));
```
## Typescript support
The package is written in Typescript and includes types by default. The `throttle` function is a generic,
and in most cases will automatically infer the right type for the result of the promise from the input.
However, you may also specify the return type when needed:
```typescript
import throttledQueue from 'throttled-queue';
const throttle = throttledQueue<number>(1, 1000);
const result1 = await throttle<string>(() => '1');
const result2 = await throttle<boolean>(() => Promise.resolve(true));
```
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