Concurrency
Requires TypeScript 5.4 or newer.
npm i ciorentbun i ciorentpnpm i ciorentyarn add ciorentFeatures
Section titled “Features”ciorent provides simple & optimized primitives for concurrency.
| Primitive | Description |
|---|---|
| Sleep | Synchronous and asynchronous sleep functions. |
| Yield | Continue the execution of an asynchronous task on next tick, allowing other tasks to run. |
| Semaphore | Control access to a common resource by multiple tasks. |
| Latch | Allow asynchronous tasks to wait until another task completes an operation before continuing execution. |
Import:
import { sleep, sleepSync, nextTick, semaphore, latch} from 'ciorent';sleep()
Section titled “sleep()”ms: number- Sleep duration in milliseconds.- Returns
Promise<void>- APromisethat resolves inmsmilliseconds.
Sleep for ms milliseconds.
sleepSync()
Section titled “sleepSync()”ms: number- Sleep duration in milliseconds.
Sleep for ms milliseconds. This blocks the event loop.
nextTick
Section titled “nextTick”Continue the async task on next microtick. Await this value is equivalent to await a resolved Promise.
semaphore
Section titled “semaphore”A primitive used to control access to a common resource by multiple tasks.
semaphore.init()
Section titled “semaphore.init()”n: number- Number of permits.- Returns
semaphore.Semaphore
Create a semaphore with n permits.
semaphore.acquire()
Section titled “semaphore.acquire()”sem: semaphore.Semaphore- A semaphore to acquire a permit.- Returns
Promise<void> | void
Acquire a permit from sem or wait until a permit is available.
semaphore.release()
Section titled “semaphore.release()”sem: semaphore.Semaphore- A semaphore to release a permit.
Release a permit to sem.
A primitive that allows one task to wait until another task completes an operation before continuing execution.
latch.init()
Section titled “latch.init()”- Returns
latch.Latch
Create a closed latch.
latch.open()
Section titled “latch.open()”c: latch.Latch- The latch to open.
Unblock tasks that are waiting for c to open.
latch.close()
Section titled “latch.close()”c: latch.Latch- The latch to close.
Reclose a latch if it was opened.
latch.wait()
Section titled “latch.wait()”c: latch.Latch- The latch to wait for.- Returns
Promise<void> | void
Wait for c to open.