Skip to main content
Module

x/async/lock.ts>Lock

🦕 Asynchronous primitive modules loosely port from Python's asyncio for Deno.
Go to Latest
class Lock
import { Lock } from "https://deno.land/x/async@v1.1.2/lock.ts";

Implements a mutex lock for Promise. Not thread-safe.

A lock can be used to guarantee exclusive access to a shared resource.

Constructors

new
Lock()

Methods

acquire(): Promise<true>

Acuire the lock.

This method waits until the lock is unlocked, sets it to locked and returns true.

When more than one coroutine is blocked in acquire() waiting for the lock to be unlocked, only one coroutine eventually proceeds.

Acquiring a lock is fair, the coroutine that proceeds will be the first coroutine that started waiting on the lock.

locked(): boolean

Return true if the lock is locked.

release(): void

Release the lock.

When the lock is locked, reset it to unlocked and return.

If the lock is unlocked, an Error is thrown.

with(callback: () => void | Promise<void>): Promise<void>

Acuire the lock and execute callback to access shared state.

This is preferred way to use a Lock.