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

1.0.0 • Public • Published

npm version Build Status Coverage Status

async-task-throttle

A simple async task throttle.

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>,
    size?: number = 6,
    max: number = Infinity
): T
 
  • task: the async task function.
  • size: defualt 6, at the same moment, up to size tasks are runing, others are in the waiting queue.
  • max: defualt Infinity, when the length of waiting queue is greater than max, late tasks will be rejected instantly.

Dependencies (0)

    Dev Dependencies (9)

    Package Sidebar

    Install

    npm i async-task-throttle

    Weekly Downloads

    15

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    11.9 kB

    Total Files

    7

    Last publish

    Collaborators

    • breeze2