Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@calebboyd/semaphore

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@calebboyd/semaphore

Simple promise based counting semaphore

latest
npmnpm
Version
1.3.1
Version published
Maintainers
1
Created
Source

Semaphore

Naive resource management / concurrency primitive

npm i @calebboyd/semaphore

Example

import { Semaphore } from '@calebboyd/semaphore'
import { something } from './somewhere'

function doSomeWork () {
  const lock = new Semaphore(10)
  while(something.isTrue) {
    lock.acquire().then(x => () => {
      something.work().then(lock.release)
    })    
  }
}

The semaphore will only allow 10 locks to be aquired at a time. This limits the concurrency of the asyncronous function something.work to 10

API:

export class Semaphore {
  constructor (count: number) {}
  acquire(): Promise<void>
  release(): void
}

License

MIT

FAQs

Package last updated on 11 May 2018

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