A promise-based semaphore implementation suitable to be used with ES7 async/await.
This package can be used to synchronize functions that span multiple iterations of the event loop and prevent other code from being executed while your function is waiting for some event.
yarn add semaphore-async-await
import Semaphore from 'semaphore-async-await';
(async () => {
// A Semaphore with one permit is a lock
const lock = new Semaphore(1);
// Helper function used to wait for the given number of milliseconds
const wait = (ms) => new Promise(r => setTimeout(r, ms));
let globalVar = 0;
(async () => {
// This waits (without blocking the event loop) until a permit becomes available
await lock.wait();
const localCopy = globalVar;
await wait(500);
globalVar = localCopy + 1;
// Signal releases the lock and lets other things run
lock.signal();
})();
// This returns false because the function above has acquired the lock
// and is scheduled to continue executing once the main function yields or
// returns
console.log(lock.tryAcquire() === false);
// Similar to the function above but using waitFor instead of wait. We
// give it five seconds to wait which is enough time for it to acquire
// the lock
(async () => {
// This waits for at least five seconds, trying to acquire a permit.
const didAcquireLock = await lock.waitFor(5000);
if (didAcquireLock) {
const localCopy = globalVar;
await wait(500);
globalVar = localCopy + 1;
// Signal releases the lock and lets other things run
lock.signal();
}
})();
// Alternative to using wait()/signal() directly
lock.execute(async () => {
const localCopy = globalVar;
await wait(500);
globalVar = localCopy + 1;
});
// Wait for everything to finish
await wait(2000);
console.log(globalVar === 3);
})();
- Semaphore(permits) ⇒
Semaphore
Creates a semaphore with the given 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, give it one permit. This number can also be negative.
- wait() ⇒
Promise
Returns a promise used to wait for a permit to become available.
- waitFor(milliseconds) ⇒
Promise
Same as wait except the promise returned gets resolved with false if no permit becomes available in time.
- tryAcquire() ⇒
boolean
Synchronous function that tries to acquire a permit and returns true if successful, false otherwise.
- signal()
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.
- execute(func) ⇒
Promise
Schedules func to be called once a permit becomes available. Returns a promise that resolves to the return value of the function.
MIT