@freedomofpress/webcat
    Preparing search index...

    Class Mutex

    A mutex for enforcing execution order in asynchronous code.

    class AsyncThing {
    #mutex = new Mutex();
    async doSomeThings(x) { // Guaranteed execution order:
    using _ = await this.#mutex.acquire(); //
    await thingNumberOne(x); // 1. t.doSomeThings(1)
    await thingNumberTwo(x); // 1.1. thingNumberOne(1)
    } // 1.2. thingNumberTwo(1)
    } // 2. t.doSomethings(2)
    // 2.1. thingNumberOne(2)
    const t = new AsyncThing(); // 2.2. thingNumberTwo(2)
    t.doSomeThings(1); // 3. t.doSomethings(3)
    t.doSomeThings(2); // 3.1. thingNumberOne(3)
    t.doSomeThings(3); // 3.2. thingNumberTwo(3)

    Implements

    Index
    • Locks this Mutex. Calling await mutex.acquire() on an unlocked Mutex returns immediately. Calling it on a locked Mutex awaits until the Mutex is released. Calling await mutex.acquire(lock) with the lock that is holding the Mutex returns immediately and returns the same lock, which is useful, for example, in recursive algorithms.

      Parameters

      • Optionallock: Lock

        An optional Lock that will be used to hold the Mutex.

      Returns Promise<Lock>

      The Lock currently holding the locked Mutex.

    • Releases this Mutex. Called either directly or by the Lock holding the Mutex as it gets disposed.

      Returns void