New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@node-libraries/semaphore

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@node-libraries/semaphore

Implement virtual semaphore for asynchronous processing

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
152
increased by33.33%
Maintainers
1
Weekly downloads
 
Created
Source

@node-libraries/semaphore

Implement virtual semaphore for asynchronous processing

usage

// Importing packages
import { semaphore } from "@node-libraries/semaphore";
// Instance Creation
const s = semaphore();
// Synchronization Lock
await s.acquire();
// Synchronization lock release
s.release();
// Wait until all locks are unlocked.
await s.all();
// Specify maximum number of parallels
const s = semaphore(5);

example

import { semaphore } from "@node-libraries/semaphore";

const f = (value: string) =>
  new Promise<void>((resolve) => {
    console.timeLog("debug", value);
    setTimeout(resolve, 1000);
  });

const main = async () => {
  console.time("debug");
  const s = semaphore();
  ["A", "B", "C", "D", "E"].forEach(async (v) => {
    await s.acquire();
    await f(v);
    s.release();
  });
  await s.all(); // Wait for everything to be finished.
  console.timeLog("debug", "end");
};
main();

/* Result
debug: 0.197ms A
debug: 1.014s B
debug: 2.027s C
debug: 3.039s D
debug: 4.040s E
debug: 5.050s end
*/
import { semaphore } from "@node-libraries/semaphore";

const f = (value: string) =>
  new Promise<void>((resolve) => {
    console.timeLog("debug", value);
    setTimeout(resolve, 1000);
  });

const main = async () => {
  console.time("debug");
  const s = semaphore(2);
  ["A", "B", "C", "D", "E"].forEach(async (v) => {
    await s.acquire();
    await f(v);
    s.release();
  });
  await s.all(); // Wait for everything to be finished.
  console.timeLog("debug", "end");
};
main();

/* Result
debug: 0.19ms A
debug: 1.826ms B
debug: 1.005s C
debug: 1.005s D
debug: 2.012s E
debug: 3.028s end
*/

Keywords

FAQs

Package last updated on 06 Dec 2023

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