API

Class AsyncLock

Provides a simple way of creating the rough equivalent of an async critical section.

const myLock = new AsyncLock();

private async MyFuncAsync(): Promise<void> {
await myLock.lockAsync(async () => {
await operation1Async();
await operation2Async();
});
}
Index
  • Executes the provided function when the lock is acquired (e.g. when the previous operation finishes).

    Type Parameters

    • T

    Parameters

    • func: () => T | Promise<T>

      The function to execute.

    • Optionalsignal: AbortSignal

      An optional signal that can be used to abort the operation.

    Returns Promise<T>

    A promise that resolves when the func finishes executing.

  • Executes the provided function when all the specified locks are acquired.

    Type Parameters

    • T

    Parameters

    • func: () => T | Promise<T>

      The function to execute.

    • locks: AsyncLock[]

      The locks to acquire.

    • Optionalsignal: AbortSignal

      An optional signal that can be used to abort the operation.

    Returns Promise<T>

    A promise that resolves when the func finishes executing.