CancellableAsyncLock interface

CancellableAsyncLock provides a mechanism for forcing tasks using the same 'key' to be executed serially.

Pending tasks can be manually cancelled via an abortSignal or automatically cancelled by reach a provided timeout value.

Methods

acquire<T>(string, (args: any[]) => Promise<T>, AcquireLockProperties)

Returns a promise that resolves to the value returned by the provided task function. Only 1 task can be invoked at a time for a given key value.

An acquire call can be cancelled via an abortSignal. If cancelled, the promise will be rejected with an AbortError.

acquireTimeoutInMs can also be provided to properties. If the timeout is reached before the provided task is invoked, then the promise will be rejected with an Error stating the task timed out waiting to acquire a lock.

Method Details

acquire<T>(string, (args: any[]) => Promise<T>, AcquireLockProperties)

Returns a promise that resolves to the value returned by the provided task function. Only 1 task can be invoked at a time for a given key value.

An acquire call can be cancelled via an abortSignal. If cancelled, the promise will be rejected with an AbortError.

acquireTimeoutInMs can also be provided to properties. If the timeout is reached before the provided task is invoked, then the promise will be rejected with an Error stating the task timed out waiting to acquire a lock.

function acquire<T>(key: string, task: (args: any[]) => Promise<T>, properties: AcquireLockProperties): Promise<T>

Parameters

key

string

All acquire calls are grouped by the provided key.

task

(args: any[]) => Promise<T>

The function to invoke once the lock has been acquired.

properties
AcquireLockProperties

Additional properties to control the behavior of acquire.

Returns

Promise<T>