MCPcopy Create free account
hub / github.com/denoland/std / tryAcquire

Method tryAcquire

async/unstable_semaphore.ts:114–120  ·  view source on GitHub ↗

* Tries to acquire a permit without waiting. * * @example Usage * ```ts no-assert * import { Semaphore } from "@std/async/unstable-semaphore"; * * const sem = new Semaphore(1); * const permit = sem.tryAcquire(); * if (permit) { * using _ = permit; * // critical sect

()

Source from the content-addressed store, hash-verified

112 * @returns A {@linkcode Disposable} if a permit was acquired, `undefined` otherwise.
113 */
114 tryAcquire(): Disposable | undefined {
115 if (this.#count > 0) {
116 this.#count--;
117 return { [Symbol.dispose]: () => this.release() };
118 }
119 return undefined;
120 }
121
122 /**
123 * Releases a permit, allowing the next waiter to proceed.

Callers 1

Calls 1

releaseMethod · 0.95

Tested by

no test coverage detected