asynchronous-lock
v1.1.0
Published
Provide multiple asynchronous locks.
Downloads
13
Readme
Asynchronous Lock
This package provides multiple locks for synchronizing asynchronous processes
Install
pnpm add asynchronous-lock
Why Need Lock
We must know why do we need locks in JavaScript which is a single-threaded language, here is an example
Mock a database that has two asynchronous functions
read
andwrite
class Database { private count: number; private constructor() { this.count = 0; } static create(): Database { return new Database(); } async read(): Promise<number> { return this.count; } async write(count: number) { this.count = count; } }
Define
increase
function that can increasecount
in databaseasync function increase(database: Database) { const count = await database.read(); await database.write(count + 1); }
Test asynchronously
async function test() { // Create a database const database = Database.create(); // Execute asynchronously await Promise.all([ increase(database), increase(database), increase(database), increase(database), increase(database), ]); // Check result const count = await database.read(); console.log(count); // Unexpected result 1 (expect to be 5) }
Asynchronous executions may cause unexpected result, that's why we need locks in JavaScript
Usage
Lock
: See Lock.spec.tsResourceLock
: See ResourceLock.spec.tsResourcesLock
: See ResourcesLock.spec.ts