Skip to content

Function: withProcessLock()

withProcessLock<T>(key, fn): Promise<T>

Run fn while holding the lock named by key, releasing it when fn settles.

Callers sharing a key are serialized across the main thread and every worker started through processLockWorkerOptions; distinct keys never block each other. Returns whatever fn returns and propagates what it throws, so it drops into an existing expression without restructuring.

key is any value with a stable identity - a string, a ["invoice", id] tuple, a config object - canonicalized by object.toStableKey, the same rule @dbx-tools/postgres uses for advisory-lock ids and channel names. Structure is part of the identity: ["invoice", 7] and "invoice_7" are different locks.

T

unknown

() => T | Promise<T>

Promise<T>

await withProcessLock(["cache", name], async () => {
if (!(await exists(name))) await build(name);
});