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

async-await-mutex-lock

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-await-mutex-lock

Mutex locks for async functions with functionality to use keys for separate locks

  • 1.0.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.2K
decreased by-14.45%
Maintainers
0
Weekly downloads
 
Created
Source

Async Await Mutex Lock

Tests

Mutex locks for async functions with functionality to use keys for separate locks

NPM

Usage Instructions

Without Key
import { Lock } from "async-await-mutex-lock";

let lock = new Lock();

async function serialTask() {
  await lock.acquire();

  try {
    // Don't return a promise here as Promise may resolve after finally
    // has executed
  } finally {
    lock.release();
  }
}
With Key

All the keys will have their own separate locks and separate waiting lists. A key can have any type (eg. string, number, etc. or a custom type allowed by typescript as a Map key)

import { Lock } from "async-await-mutex-lock";

let lock = new Lock<string>();

async function serialTask() {
  await lock.acquire("myKey");

  try {
    // Don't return a promise here as Promise may resolve after finally
    // has executed
  }
  finally {
    lock.release("myKey");
  }
}

async function serialTaskTwo() {
  await lock.acquire("myKeyTwo");

  try {
    // Don't return a promise here as Promise may resolve after finally
    // has executed
  }
  finally {
    lock.release("myKeyTwo");
  }
}
Checking if a lock is acquired or not
import { Lock } from "async-await-mutex-lock";

let lock = new Lock();

async function serialTask() {
  await lock.acquire();

  console.log(lock.isAcquired()); // prints true
}

isAcquired() with key checks for the given key separately.

import { Lock } from "async-await-mutex-lock";

let lock = new Lock<string>();

async function serialTask() {
  await lock.acquire("myKey");

  console.log(lock.isAcquired("myKey")) // prints true
}
Issues or Bugs

In case of any issues or bugs, please open a pull request here

Keywords

FAQs

Package last updated on 13 Nov 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