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

GitHub

View Github