throttled-queue
Advanced tools
Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "throttled-queue", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests.", | ||
@@ -18,3 +18,3 @@ "main": "throttled-queue.js", | ||
"node", | ||
"throttled-queue", | ||
"rate limit", | ||
"queue", | ||
@@ -25,3 +25,3 @@ "throttle", | ||
], | ||
"author": "Shaun Persad <shaunpersad@gmail.com> (http://inspirationjs.io)", | ||
"author": "Shaun Persad <shaunpersad@gmail.com>", | ||
"license": "MIT", | ||
@@ -28,0 +28,0 @@ "bugs": { |
# throttled-queue | ||
This utility function allows throttling of a queue any arbitrary code. For example, making network calls to popular APIs | ||
such as Twitter is subject to rate limits. By wrapping all of your API calls in a throttle, it will automatically adjust | ||
your requests to be within the acceptable rate limits. | ||
Throttles arbitrary code to execute a maximum number of times per interval. Best for making throttled API requests. | ||
For example, making network calls to popular APIs such as Twitter is subject to rate limits. By wrapping all of your API calls in a throttle, it will automatically adjust your requests to be within the acceptable rate limits. | ||
Unlike the `throttle` functions of popular libraries like lodash and underscore, `throttled-queue` will not prevent any executions. Instead, every execution is placed into a queue, which will be drained at the desired rate limit. | ||
## Installation | ||
@@ -16,3 +18,3 @@ Can be used in a Node.js environment, or directly in the browser. | ||
1) If in node.js, `require` the factory function: | ||
``` | ||
```js | ||
var throttledQueue = require('throttled-queue'); | ||
@@ -24,7 +26,7 @@ ``` | ||
and the interval in milliseconds as the second: | ||
``` | ||
```js | ||
var throttle = throttledQueue(5, 1000); // at most 5 requests per second. | ||
``` | ||
3) Use the `throttle` instance as a function to enqueue actions: | ||
``` | ||
```js | ||
throttle(function() { | ||
@@ -38,3 +40,3 @@ // perform some type of activity in here. | ||
Rapidly assigning network calls to be run, but they will be limited to 1 request per second. | ||
``` | ||
```js | ||
var throttledQueue = require('throttled-queue'); | ||
@@ -47,2 +49,3 @@ var throttle = throttledQueue(1, 1000); // at most make 1 request every second. | ||
// make a network request. | ||
fetch('https://api.github.com/search/users?q=shaunpersad').then(console.log); | ||
}); | ||
@@ -54,3 +57,3 @@ } | ||
and be subject to the same rate limits. | ||
``` | ||
```js | ||
var throttledQueue = require('throttled-queue'); | ||
@@ -63,2 +66,3 @@ var throttle = throttledQueue(1, 60 * 1000); // at most make 1 request every minute. | ||
// make a network request. | ||
fetch('https://api.github.com/search/users?q=shaunpersad').then(console.log); | ||
}); | ||
@@ -70,2 +74,3 @@ } | ||
// make another type of network request. | ||
fetch('https://api.github.com/search/repositories?q=throttled-queue+user:shaunpersad').then(console.log); | ||
}); | ||
@@ -76,3 +81,3 @@ } | ||
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'); | ||
@@ -85,2 +90,3 @@ var throttle = throttledQueue(10, 1000); // at most make 10 requests every second. | ||
// This will fire at most 10 a second, as rapidly as possible. | ||
fetch('https://api.github.com/search/users?q=shaunpersad').then(console.log); | ||
}); | ||
@@ -91,3 +97,3 @@ } | ||
You can space out your actions by specifying `true` as the third (optional) parameter: | ||
``` | ||
```js | ||
var throttledQueue = require('throttled-queue'); | ||
@@ -100,2 +106,3 @@ var throttle = throttledQueue(10, 1000, true); // at most make 10 requests every second, but evenly spaced. | ||
// 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); | ||
}); | ||
@@ -102,0 +109,0 @@ } |
Sorry, the diff of this file is not supported yet
60432
110
16