Socket
Socket
Sign inDemoInstall

semaphore-async-await

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

semaphore-async-await

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


Version published
Weekly downloads
80K
decreased by-7.9%
Maintainers
1
Weekly downloads
 
Created
Source

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(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.

License

MIT

FAQs

Package last updated on 16 Dec 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc