Skip to content

Function: withFileLock() ​

ts
function withFileLock<T>(
   lockPath, 
   work, 
   options?
): Promise<T>;

Defined in: with-file-lock.ts:18

Executes an asynchronous work callback while holding an exclusive file lock on lockPath.

Guarantees that the acquired FileLockLease is released inside a finally block even when work throws or rejects.

Type Parameters ​

Type ParameterDescription
TReturn value type of work.

Parameters ​

ParameterTypeDescription
lockPathstringFilesystem path of the lockfile to acquire.
work() => Promise<T>Asynchronous critical-section callback to execute under the lock.
options?FileLockOptionsOptional timeout, stale threshold, heartbeat, and retry settings.

Returns ​

Promise<T>

Result resolved by work.

Released under the MIT License.