A mutex is a task queue where at most one task is active at a time.

Implements

  • PromiseLike<unknown>

Constructors

Methods

  • Execute a task immediately if it is a function.

    Parameters

    • task: PromiseLike<unknown> | (() => PromiseLike<unknown>)

    Returns Promise<unknown>

  • Enqueue additional work.

    If task is a function it runs when current activity completes. If it is a promise then the mutex will not clear until task resolves.

    Parameters

    • task: PromiseLike<unknown> | (() => PromiseLike<unknown>)
    • Optionalcancel: (() => void)
        • (): void
        • Returns void

    Returns void

  • Cancel remaining work and perform one last task with the Mutex held.

    Parameters

    • Optionalcleanup: (() => PromiseLike<void>)
        • (): PromiseLike<void>
        • Returns PromiseLike<void>

    Returns void

  • As a PromiseLike, you can await the Mutex. This promise resolves when current activity completes but the mutex may engage in another activity immediately thereafter. So the mutex is not guaranteed to be available after an await.

    Type Parameters

    • TResult1 = void
    • TResult2 = never

    Parameters

    • Optionalonfulfilled: null | ((value: unknown) => TResult1 | PromiseLike<TResult1>)
    • Optionalonrejected: null | ((reason: any) => TResult2 | PromiseLike<TResult2>)

    Returns PromiseLike<TResult1 | TResult2>