async-task-throttle-on-response
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

npm version Build Status Coverage Status

async-task-throttle-on-response

We refer lib https://github.com/breeze2/async-task-throttle Also, we modify async-task-throttle to support rate limit on response

Install

$ yarn add async-task-throttle

Usage

Sample

import AsyncTaskThrottle from 'async-task-throttle'


function task (url) {
    return fetch(url)
}

const throttleTask = AsyncTaskThrottle.create(task, 6, 100)

// use `throttleTask` just like `task`
// but up to 6 tasks are running at the same moment
throttleTask('https://github.com/breeze2/markdown-it-all').then(value => {
    console.log(value)
}).catch(error => {
    console.error(error)
})

Interface

AsyncTaskThrottle.create

function AsyncTaskThrottle.create(
    task: T extends (...args: any[]) => Promise<any>,
    rateLimitCount?: number = 1,
    rateLimitDuration?: number = 5000,
    max: number = Infinity
): T
  • task: the async task function.
  • rateLimitCount: defualt 1, at the same moment, up to rateLimitCount tasks are runing, others are in the waiting queue.
  • rateLimitDuration: default 5000, make rateLimitCount request every rateLimitDuration.

Note: Timer from last asynchronous task resolution time

  • max: defualt Infinity, when the length of waiting queue is greater than max, late tasks will be rejected instantly.

Package Sidebar

Install

npm i async-task-throttle-on-response

Weekly Downloads

11

Version

1.0.1

License

MIT

Unpacked Size

20.3 kB

Total Files

10

Last publish

Collaborators

  • caothien