Skip to content

Concurrency

Requires TypeScript 5.4 or newer.

Terminal window
npm i ciorent

ciorent provides simple & optimized primitives for concurrency.

PrimitiveDescription
SleepSynchronous and asynchronous sleep functions.
YieldContinue the execution of an asynchronous task on next tick, allowing other tasks to run.
SemaphoreControl access to a common resource by multiple tasks.
LatchAllow asynchronous tasks to wait until another task completes an operation before continuing execution.

Import:

import {
sleep, sleepSync, nextTick,
semaphore, latch
} from 'ciorent';
  • ms: number - Sleep duration in milliseconds.
  • Returns Promise<void> - A Promise that resolves in ms milliseconds.

Sleep for ms milliseconds.

  • ms: number - Sleep duration in milliseconds.

Sleep for ms milliseconds. This blocks the event loop.

Continue the async task on next microtick. Await this value is equivalent to await a resolved Promise.

A primitive used to control access to a common resource by multiple tasks.

  • n: number - Number of permits.
  • Returns semaphore.Semaphore

Create a semaphore with n permits.

  • 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.

  • 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.

  • Returns latch.Latch

Create a closed latch.

  • c: latch.Latch - The latch to open.

Unblock tasks that are waiting for c to open.

  • c: latch.Latch - The latch to close.

Reclose a latch if it was opened.

  • c: latch.Latch - The latch to wait for.
  • Returns Promise<void> | void

Wait for c to open.