@esfx/async-mutex Package
Provides the AsyncMutex class, an async coordination primitive.
Installation
npm i @esfx/async-mutex
Usage
import { AsyncMutex } from "@esfx/async-mutex";
const m = new AsyncMutex();
let counter = 0;
async function worker() {
    for (let i = 0; i < 3; i++) {
        // get exclusive access to 'm', which protects 'counter'.
        const lk = await m.lock();
        try {
            const current = counter;
            await doSomethingElse();
            // we still have exclusive access to 'm', which protects 'counter'.
            counter = current + 1;
        }
        finally {
            // release the lock
            lk.unlock();
        }
    }
}
async function main() {
    // start two workers that share a resource
    await Promise.all([worker(), worker()]);
    counter; // 6
}
Classes
AsyncMutex
An async coordination primitive used to coordinate access to a protected resource.