Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • Throttle

Index

Constructors

constructor

  • new Throttle(requestsPerPeriod?: number, periodLength?: number, errorOnLimit?: boolean, PromiseImplementation?: PromiseConstructorLike): Throttle
  • Parameters

    • Default value requestsPerPeriod: number = 10

      Number of requests to execute in given period

    • Default value periodLength: number = -1

      Number of milliseconds in period, set to -1 for concurrency

    • Default value errorOnLimit: boolean = false

      Error if number of requests exceeds limits

    • Default value PromiseImplementation: PromiseConstructorLike = Promise

      Promise library of your choice

    Returns Throttle

Properties

PromiseImplementation

PromiseImplementation: PromiseConstructorLike

Promise library of your choice

Protected _nRequests

_nRequests: number = 0

Protected _pending

_pending: PendingRequest[] = []

Protected _tick

_tick: Timeout | undefined = undefined

Protected _times

_times: number[] = []

errorOnLimit

errorOnLimit: boolean

Error if number of requests exceeds limits

periodLength

periodLength: number

Number of milliseconds in period, set to -1 for concurrency

requestsPerPeriod

requestsPerPeriod: number

Number of requests to execute in given period

Methods

Private _execute

  • _execute(): Promise<void>

Private _testTick

  • _testTick(): Promise<void>

acquire

  • acquire<T>(fn: () => T): Promise<T>
  • Acquire position in queue

    Type parameters

    • T

    Parameters

    • fn: () => T

      Function to execute

        • (): T
        • Returns T

    Returns Promise<T>

    Promise

    Example (async/await):

    const results = await throttle.acquire(async () => {
        // do some async work
    
        return { hello: 'world' };
    });
    
    console.log(results); // { hello: 'world' }

    Example (Promises):

    return throttle.acquire(() => {
        // do some work
    
        return { hello: 'world' };
    }).then((results) => {
        console.log(results); // { hello: 'world' }
    });

clear

  • clear(): this
  • Clears pending queue without execution

    Returns this

flush

  • flush(): Promise<any[]>
  • Executes entire pending queue at once

    Returns Promise<any[]>

Legend

  • Class
  • Constructor
  • Property
  • Method
  • Protected property
  • Private method

Generated using TypeDoc