1.0.5 • Published 12 months ago

js-mutex v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

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);
}
1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago