async-named-mutex
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

async-named-mutex

Named mutex locks for async functions.

Usage

TypeScript:

import {MutexRealm} from "async-named-mutex";

const mutexRealm = new MutexRealm<string>();

async function processResource (resourceId: string) {
   const mutex = mutexRealm.createMutex(resourceId);
   try {
      await mutex.acquire();
      // ... process resource ...
   } finally {
      mutex.release();
   }
}

JavaScript:

import {MutexRealm} from "async-named-mutex";

const mutexRealm = new MutexRealm();

async function processResource (resourceId) {
   const mutex = mutexRealm.createMutex(resourceId);
   try {
      await mutex.acquire();
      // ... process resource ...
   } finally {
      mutex.release();
   }
}

Any value which is a valid Map index (string, number, object, ...) can be used as a name (aka key) for a mutex.

NPM package: async-named-mutex

Package Sidebar

Install

npm i async-named-mutex

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

7.51 kB

Total Files

6

Last publish

Collaborators

  • chdh