Skip to content

Named mutex locks for async functions.

License

Notifications You must be signed in to change notification settings

chdh/async-named-mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

async-named-mutex

Named mutex locks for async functions.

In asynchronous JavaScript program code, multiple async functions can operate on the same data. Named mutexes can be used to protect access to shared resources.

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