Skip to main content

Home > flowp > Mutex > tryLock

Mutex.tryLock() method​

synchronosly acquire lock

Signature:

tryLock(): MutexGuard<V>;

Returns:

MutexGuard<V>

MutexGuard - a function to release the lock, you can access wrapped value using MutexGuard.value before release

Exceptions​

Error if failed to acquire lock

Example​

const mutex = new Mutex({ a: 1 })
const { release, value } = mutex.tryLock()
const ref = value // value is a temporary reference which does not equal the value stores in mutex
ref.a // => 1
release()
ref.a // => TypeError, temporary reference destroyed