Loading problem…
Implement two async coordination primitives: a semaphore and a mutex.
In JavaScript, many async tasks run "at the same time" from your app's point of view. Sometimes you need to control access to shared resources:
N concurrent tasks.Your goal is to build both primitives so callers can wait for access and release it safely.
Implement:
Semaphore API:
Mutex API:
Behavior:
maxPermits must be a positive integer. For zero or any other invalid capacity, createSemaphore must throw an Error synchronously.maxPermits concurrent holdersrelease / unlock must be idempotent (safe to call multiple times)createMutex() should enforce exclusive access (equivalent to semaphore with 1 permit)