WithMutex
withMutex
Convenience function to execute a function with exclusive access using the default mutex.
Example
const controller = new AbortController();
const result = await withMutex('shared-resource', async () => {
// This code runs with exclusive access
return await updateSharedResource();
}, 30000, controller.signal);
controller.abort(); // Cancels the lock acquisition
Signature
function withMutex<T>(key: string, fn: () => Promise<T> | T, timeout?: number, signal?: AbortSignal): Promise<T>
Parameters
key
parameter
string
fn
parameter
() => Promise<T> | T
timeout
parameter
number
signal
parameter
AbortSignal