UNPKG

@stryke/helpers

Version:

A package containing miscellaneous helper functions that are used across many different Storm Software projects.

2 lines (1 loc) 337 B
export class Semaphore{capacity;available;deferredTasks=[];constructor(e){this.capacity=e,this.available=e}async acquire(){if(this.available>0){this.available--;return}return new Promise(e=>{this.deferredTasks.push(e)})}release(){const e=this.deferredTasks.shift();if(e!=null){e();return}this.available<this.capacity&&this.available++}}