The type of the protected value.
Readonly[toThe well-known symbol Symbol.toStringTag used by Object.prototype.toString().
Returns 'Mutex' so that Object.prototype.toString.call(mutex) produces '[object Mutex]'.
Acquires the lock and returns a copy of the protected value.
This is a convenience method equivalent to withLock(v => v).
A promise that resolves to a copy of the value.
Returns true if the mutex is currently locked.
Note: This is a snapshot and may change immediately after the call.
Acquires the lock and returns a guard for manual control.
Use this when you need more control over when to release the lock.
Important: Always release the lock in a finally block to prevent deadlocks.
A promise that resolves to a guard providing access to the value.
Acquires the lock and sets a new value.
This is a convenience method equivalent to withLock(() => { value = newValue; }).
The new value to set.
A promise that resolves when the value has been set.
Attempts to acquire the lock without waiting.
Returns immediately with Some(guard) if the lock is available,
or None if it's currently held by another operation.
Some(guard) if acquired, None if locked.
Acquires the lock and executes the callback with the protected value.
This is the recommended way to use the mutex as it automatically releases the lock when the callback completes (or throws).
The return type of the callback.
A promise that resolves to the callback's return value.
An async mutual exclusion primitive for protecting shared data.
This mutex provides exclusive access to the contained value, ensuring that only one async operation can access it at a time. This is useful for preventing race conditions in async code.
Unlike Rust's Mutex which is for multi-threading, this JavaScript version serializes async operations in the single-threaded event loop.
Since
1.6.0
See
https://doc.rust-lang.org/std/sync/struct.Mutex.html
Example