Free or 'V' operation.
If there are any promises returned by {@link take()} still awaiting their fulfillment, exactly of them is fulfilled. The order of fulfillment is implementation-defined.
Take or 'P' operation.
A promise which is fulfilled when >= 1 of the resource represented by the internal counter is available. This might be the case either if free is called or if the semaphore is initialized with a value > 0.
Take ('P') if a resource is still available (<=> counter >= 1). Otherwise, do not block (as take does) and return false.
True if a resource could be taken, False otherwise.
Try taking ('P') a resource within the next millis
milliseconds.
A promise fulfilling with true
if a resource could be taken or
false
otherwise.
Generated using TypeDoc
General counting semaphore with Promises.
It allows arbitrary take (also referred to as "P") and free ("V") calls.
To use implementations with the normal JS execution model (single-threaded + queue), take and free are supposed to be called on different 'fibers'. For example, take can be awaited in the business logic processing data while free is called inside a filesystem API callback putting the data into an array.
All of the following examples assume a semaphore initialized with a count of 0.