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

await-lock

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

await-lock

Mutex locks for async functions

  • 2.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
132K
increased by14.6%
Maintainers
1
Weekly downloads
 
Created

What is await-lock?

The await-lock npm package provides a simple mechanism for managing asynchronous locks in JavaScript. It allows you to ensure that only one asynchronous operation can proceed at a time, which is useful for managing resources that should not be accessed concurrently.

What are await-lock's main functionalities?

Basic Locking

This feature allows you to create a lock and use it to ensure that a critical section of code is not executed concurrently by multiple asynchronous functions. The code sample demonstrates acquiring a lock before entering a critical section and releasing it afterward.

const AwaitLock = require('await-lock');
const lock = new AwaitLock();

async function criticalSection() {
  await lock.acquireAsync();
  try {
    // Perform operations that require exclusive access
  } finally {
    lock.release();
  }
}

async function run() {
  await Promise.all([criticalSection(), criticalSection()]);
}

run();

Try Lock

The try lock feature allows you to attempt to acquire a lock without waiting. If the lock is already held, the operation can proceed without blocking. This is useful for scenarios where you want to perform an operation only if it can be done immediately.

const lock = new AwaitLock();

async function tryLockExample() {
  if (lock.tryAcquire()) {
    try {
      // Perform operations that require exclusive access
    } finally {
      lock.release();
    }
  } else {
    console.log('Lock is already acquired by another operation.');
  }
}

tryLockExample();

Other packages similar to await-lock

Keywords

FAQs

Package last updated on 29 Apr 2022

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