@26brains/throttled-queue-decorator 中文文档教程

发布于 5年前 浏览 28 更新于 3年前

Throttled Queue Decorator

Install

npm i @26brains/throttled-queue-decorator

Usage

const { createThrottle } = require('throttled-queue-decorator');

const throttle = createThrottle(2, 1000); // 2 requests per 1000 milliseconds

const func = (id) => axios.get(`http://example.com/record/${id}`);
const throttledFunc = throttle(func);

const main = async () => {
  return await Promise.all([
    throttledFunc(1),
    throttledFunc(2),
    throttledFunc(3),
  ])
}
//third request will be delayed by 1 second
main();

Tests

npm test

Example

node example/example.js

Credits

单个setTimeout()的使用代码> 从 https://github.com/shaunpersad/throttled-queue 复制的事件。

Throttled Queue Decorator

Install

npm i @26brains/throttled-queue-decorator

Usage

const { createThrottle } = require('throttled-queue-decorator');

const throttle = createThrottle(2, 1000); // 2 requests per 1000 milliseconds

const func = (id) => axios.get(`http://example.com/record/${id}`);
const throttledFunc = throttle(func);

const main = async () => {
  return await Promise.all([
    throttledFunc(1),
    throttledFunc(2),
    throttledFunc(3),
  ])
}
//third request will be delayed by 1 second
main();

Tests

npm test

Example

node example/example.js

Credits

Use of single setTimeout() event copied from https://github.com/shaunpersad/throttled-queue.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文