@slimio/lock
v1.0.0
Published
SlimIO Asynchronous Lock Handler
Downloads
161
Readme
Lock
SlimIO Asynchronous Handler Mutex "Like" Lock. This package has been created to easily lock parallel execution of JavaScript Asynchronous function.
Requirements
- Node.js v12 or higher
Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @slimio/lock
# or
$ yarn add @slimio/lock
Usage example
const Lock = require("@slimio/lock");
const asyncLocker = new Lock({ maxConcurrent: 3 });
async function npmInstall() {
const free = await asyncLocker.acquireOne();
try {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log("npm install resolved!");
}
finally {
free();
}
}
// Run 3 per 3 methods
Promise.all([
npmInstall(),
npmInstall(),
npmInstall(),
npmInstall(),
npmInstall()
]).then(() => console.log("all done!")).catch(console.error);
API
Properties
declare class Lock {
public readonly max: number;
public readonly running: number;
}
Methods
Dependencies
This project have no dependencies.
License
MIT