Socket
Socket
Sign inDemoInstall

async-sema

Package Overview
Dependencies
Maintainers
61
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-sema

Semaphore using `async` and `await`


Version published
Maintainers
61
Created

What is async-sema?

The async-sema package is a lightweight semaphore implementation for async/await in JavaScript. It is designed to control concurrent access to a common resource by multiple asynchronous operations. This is particularly useful in scenarios where you need to rate-limit access to an API, manage concurrent database transactions, or simply ensure that a certain section of your code does not get overwhelmed with too many concurrent executions.

What are async-sema's main functionalities?

Semaphore

This feature allows you to create a semaphore that limits the number of concurrent asynchronous operations. In the code sample, a semaphore with a concurrency limit of 1 is created, meaning only one async operation can enter the critical section at a time. This is useful for rate-limiting or ensuring that certain operations do not run concurrently.

const { Sema } = require('async-sema');
const s = new Sema(1); // Allow 1 concurrent async call
async function criticalSection() {
  await s.acquire();
  try {
    // Your async logic here
  } finally {
    s.release();
  }
}

RateLimiter

This feature implements a rate limiter, which is useful for controlling the rate at which functions are executed. In the example, a rate limiter is set to allow 5 operations per interval. This is particularly useful for API rate limiting or controlling the execution rate of any set of operations.

const { RateLimit } = require('async-sema');
const rl = new RateLimit(5); // 5 operations per interval
async function limitedOperation() {
  await rl.acquire();
  // Your async operation here
}

Other packages similar to async-sema

Keywords

FAQs

Package last updated on 17 Aug 2021

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