semaphore-async-await
Options
All
  • Public
  • Public/Protected
  • All
Menu

Class representing a semaphore Semaphores are initialized with a number of permits that get aquired and released over the lifecycle of the Semaphore. These permits limit the number of simultaneous executions of the code that the Semaphore synchronizes. Functions can wait and stop executing until a permit becomes available.

Locks that only allow one execution of a critical section are a special case of Semaphores. To construct a lock, initialize a Semaphore with a permit count of 1.

This Semaphore class is implemented with the help of promises that get returned by functions that wait for permits to become available. This makes it possible to use async/await to synchronize your code.

Hierarchy

Index

Constructors

constructor

  • Creates a semaphore.

    Parameters

    • permits: number

      The number of permits, i.e. things being allowed to run in parallel. To create a lock that only lets one thing run at a time, set this to 1. This number can also be negative.

    Returns Semaphore

Properties

permits

permits: number

promiseResolverQueue

promiseResolverQueue: Array<function> = []

Methods

acquire

  • acquire(): Promise<boolean>
  • Alias for Semaphore.wait.

    Returns Promise<boolean>

    A promise that gets resolved when execution is allowed to proceed.

drainPermits

  • drainPermits(): number
  • Acquires all permits that are currently available and returns the number of acquired permits.

    Returns number

    Number of acquired permits.

execute

  • execute<T>(func: function): Promise<T>
  • Schedules func to be called once a permit becomes available. Returns a promise that resolves to the return value of func.

    Type parameters

    • T

      The return type of func.

    Parameters

    • func: function

      The function to be executed.

        • (): T
        • Returns T

    Returns Promise<T>

    A promise that gets resolved with the return value of the function.

getPermits

  • getPermits(): number
  • Returns the number of available permits.

    Returns number

    The number of available permits.

release

  • release(): void

signal

  • signal(): void
  • Increases the number of permits by one. If there are other functions waiting, one of them will continue to execute in a future iteration of the event loop.

    Returns void

tryAcquire

  • tryAcquire(): boolean
  • Synchronous function that tries to acquire a permit and returns true if successful, false otherwise.

    Returns boolean

    Whether a permit could be acquired.

wait

  • wait(): Promise<boolean>
  • Returns a promise used to wait for a permit to become available. This method should be awaited on.

    Returns Promise<boolean>

    A promise that gets resolved when execution is allowed to proceed.

waitFor

  • waitFor(milliseconds: number): Promise<boolean>
  • Same as Semaphore.wait except the promise returned gets resolved with false if no permit becomes available in time.

    Parameters

    • milliseconds: number

      The time spent waiting before the wait is aborted. This is a lower bound, don't rely on it being precise.

    Returns Promise<boolean>

    A promise that gets resolved with true when execution is allowed to proceed or false if the time given elapses before a permit becomes available.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc