Skip to content
James Ide edited this page Aug 16, 2022 · 2 revisions

Class: AwaitLock

A mutex lock for coordination across async functions

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

new AwaitLock()

Accessors

acquired

get acquired(): boolean

Whether the lock is currently acquired or not. Accessing this property does not affect the status of the lock.

Returns

boolean

Methods

acquireAsync

acquireAsync(options?): Promise<void>

Acquires the lock, waiting if necessary for it to become free if it is already locked. The returned promise is fulfilled once the lock is acquired.

A timeout (in milliseconds) may be optionally provided. If the lock cannot be acquired before the timeout elapses, the returned promise is rejected with an error. The behavior of invalid timeout values depends on how setTimeout handles those values.

After acquiring the lock, you must call release when you are done with it.

Parameters

Name Type
options Object
options.timeout? number

Returns

Promise<void>


release

release(): void

Releases the lock and gives it to the next waiting acquirer, if there is one. Each acquirer must release the lock exactly once.

Returns

void


tryAcquire

tryAcquire(): boolean

Acquires the lock if it is free and otherwise returns immediately without waiting. Returns true if the lock was free and is now acquired, and false otherwise.

This method differs from calling acquireAsync with a zero-millisecond timeout in that it runs synchronously without waiting for the JavaScript task queue.

Returns

boolean