Skip to content

jsoendermann/semaphore-async-await

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaScript Semaphore

A promise-based semaphore implementation suitable to be used with ES7 async/await.

But JavaScript is single-threaded and doesn't need semaphores!

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.

Install

yarn add semaphore-async-await

Usage

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);
})();

Methods

Semaphore(premits)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)

Schedules func to be called once a permit becomes available.

License

MIT

About

🎌 Promise based Semaphore suitable to be used with async/await.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published