* 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
()
| 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. |
no test coverage detected