js-mutex
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Mutex

NPM version build status Test coverage npm download

A modern lightweight lib to control the promise mutex

Usage

The version of your node.js should be greater than v12

npm i -S js-mutex

Importing library

import Mutex from 'js-mutex';

async function singleExecute() {
  const mutex = new Mutex();
  let cur = 0;
  await mutex.lock();
  cur = 1;
  await mutex.unlock();
}

async function singleExecuteConcurrency() {
  const mutex = new Mutex();
  const result: number[] = [];

  async function fn(i: number) {
    await mutex.acquire(async () => {
      await sleep(100);
      result.push(i);
    });
  }

  const promises = [];
  for (let i = 0; i < 10; i += 1) {
    promises.push(fn(i));
  }

  await Promise.all(promises);
}

/js-mutex/

    Package Sidebar

    Install

    npm i js-mutex

    Weekly Downloads

    1

    Version

    1.0.5

    License

    ISC

    Unpacked Size

    10.6 kB

    Total Files

    7

    Last publish

    Collaborators

    • x-cold