@esfx/async-mutex
Provides 'AsyncMutex', an async coordination primitive.
Last updated a year ago by rbuckton .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @esfx/async-mutex 
SYNC missed versions from official npm registry.

@esfx/async-mutex

Provides AsyncMutex, an async coordination primitive.

Overview

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
}

API

You can read more about the API here.

Current Tags

  • 1.0.0-pre.13                                ...           latest (a year ago)

8 Versions

  • 1.0.0-pre.13                                ...           a year ago
  • 1.0.0-pre.12                                ...           a year ago
  • 1.0.0-pre.11                                ...           a year ago
  • 1.0.0-pre.10                                ...           a year ago
  • 1.0.0-pre.9                                ...           a year ago
  • 1.0.0-pre.8                                ...           a year ago
  • 1.0.0-pre.7                                ...           a year ago
  • 1.0.0-pre.6                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (6)
Dev Dependencies (0)
None

Copyright 2014 - 2016 © taobao.org |