Class Queue<Options>

A Queue to schedule function calls for later

Example

import { queue } from "@rjweb/utils"

const mails = new queue.Queue({
wait: 1000
})

mails.add(() => sendMail()) // will execute after 0ms
mails.add(() => sendMail()) // will execute after 1000ms
mails.add(() => sendMail()) // will execute after 2000ms
mails.add(() => sendMail()) // will execute after 3000ms
mails.add(() => sendMail()) // will execute after 4000ms
mails.add(() => sendMail()) // will execute after 5000ms
mails.add(() => sendMail()) // will execute after 6000ms

Since

1.4.0

Supports

nodejs, browser

Type Parameters

  • const Options extends {
        wait?: number;
    }

Constructors

Properties

fnWaiters: [fn: Function, callback: ((type, data) => void)][] = []
lastRun: number = 0
nextRunScheduled: boolean
onError: null | ((fn, error) => any) = null

Type declaration

    • (fn, error): any
    • Parameters

      • fn: Function
      • error: unknown

      Returns any

onFinish: null | ((fn, result) => any) = null

Type declaration

    • (fn, result): any
    • Parameters

      • fn: Function
      • result: unknown

      Returns any

queue: Function[] = []

Methods

  • Add a function to run to the queue

    Parameters

    • fn: Function

    Returns this

    Since

    1.4.0

  • Add & Wait for the function to be finished

    Type Parameters

    • Fn extends Function

    Parameters

    Returns Promise<Awaited<ReturnType<Fn>>>

    Since

    1.4.1

  • Callback for when a function call finishes in an error

    Parameters

    • callback: ((fn, error) => any)
        • (fn, error): any
        • Parameters

          • fn: Function
          • error: unknown

          Returns any

    Returns this

    Since

    1.4.0

  • Callback for when a function call finishes successfully

    Parameters

    • callback: ((fn, result) => any)
        • (fn, result): any
        • Parameters

          • fn: Function
          • result: unknown

          Returns any

    Returns this

    Since

    1.4.0

  • Get the Wait Duration of this Queue

    Returns Options["wait"] extends number
        ? any[any]
        : 5000

    Since

    1.4.0

  • Returns Promise<void>