Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@chalker/queue

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chalker/queue

A trivial queue / mutex lock implementation

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@chalker/queue

A trivial Queue / Lock implementation that I used in my personal projects.

Example: https://github.com/nodejs/Gzemnid/blob/main/src/queue.js

Queue

Usage:

import { Queue } from '@chalker/queue'

const queue = new Queue(10) // concurrency

async function run() {
  await queue.claim()
  // do async stuff with controlled maximum concurrency
  queue.release()
}

Lock

new Lock() is just an alias to new Queue(1), which can be used

import { Queue } from '@chalker/queue'

const lock = new Lock()

async function run() {
  await lock.claim()
  // do async stuff with maximum concurrency = 1
  lock.release()
}

Full example

import { Queue } from '@chalker/queue'

const queue = new Queue(3)

let concurrency = 0 // Just for testing purposes
async function run(x) {
  await queue.claim()
  console.log(`Start #${x}, concurrency: ${++concurrency}`)
  await new Promise(resolve => setTimeout(resolve, 1))
  console.log(`End #${x}, concurrency: ${concurrency--}`)
  queue.release()
}

const args = new Array(6).fill(0).map((_, i) => i + 1)
await Promise.all(args.map(run))

Output:

Start #1, concurrency: 1
Start #2, concurrency: 2
Start #3, concurrency: 3
End #1, concurrency: 3
Start #4, concurrency: 3
End #2, concurrency: 3
Start #5, concurrency: 3
End #3, concurrency: 3
Start #6, concurrency: 3
End #4, concurrency: 3
End #5, concurrency: 2
End #6, concurrency: 1

License

MIT

Keywords

FAQs

Package last updated on 15 Jun 2024

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