Core
Framework v2.x
2

wait #

Available since 2.19.1
Copied to clipboard!
async wait ({ attemptDelay = this.attemptDelay, timeout = this.timeout }): Promise<boolean> {

Wait for the ressource specified in the promise to be unlocked.


Arguments Type Description
options
object
(optional) Mutex override options (see below)

options #

The options object is used to override some mutex properties for this function. The following properties can be provided:

Option Type Description
attemptDelay
number
(default: this.attemptDelay) Delay in milliseconds between resource check attempts
timeout
number
(default: this.timeout) Mutex check attempt timeout, in milliseconds. If set to 0, wait will return immediately, whether it the resource is locked within the 1st attempt. If set to -1, the mutex will wait for the resource to be free indefinitely

Returns #

True if the ressource has been unlocked before the timeout


Usage #

Waiting indefinitely until a resource is available:

Copied to clipboard!
import { Mutex } from 'kuzzle';

async function ConcurrentAccessMethod () {
  const mutex = new Mutex(
    'concurrentAccessMethodResource', { timeout: 0 });

  if (await mutex.lock())) {
      try {
        // Initialize shared stuff
      }
      finally {
        await mutex.unlock();
      }
    }
  }
  else {
    mutex.wait({ timeout: -1 });   // Wait for shared stuff to be initialized
  }
}