https://github.com/chdh/async-named-mutex
Named mutex locks for async functions.
https://github.com/chdh/async-named-mutex
async concurrency critical-section javascript lock mutex
Last synced: 9 months ago
JSON representation
Named mutex locks for async functions.
- Host: GitHub
- URL: https://github.com/chdh/async-named-mutex
- Owner: chdh
- License: mit
- Created: 2022-04-21T02:03:25.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-24T20:04:01.000Z (about 4 years ago)
- Last Synced: 2025-01-31T15:16:18.870Z (over 1 year ago)
- Topics: async, concurrency, critical-section, javascript, lock, mutex
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# async-named-mutex
Named [mutex](https://en.wikipedia.org/wiki/Lock_(computer_science)) locks for async functions.
In asynchronous JavaScript program code, multiple async functions can operate on the same data.
Named mutexes can be used to protect access to shared resources.
## Usage
TypeScript:
```typescript
import {MutexRealm} from "async-named-mutex";
const mutexRealm = new MutexRealm();
async function processResource (resourceId: string) {
const mutex = mutexRealm.createMutex(resourceId);
try {
await mutex.acquire();
// ... process resource ...
} finally {
mutex.release();
}
}
```
JavaScript:
```javascript
import {MutexRealm} from "async-named-mutex";
const mutexRealm = new MutexRealm();
async function processResource (resourceId) {
const mutex = mutexRealm.createMutex(resourceId);
try {
await mutex.acquire();
// ... process resource ...
} finally {
mutex.release();
}
}
```
Any value which is a valid [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) index
(`string`, `number`, `object`, ...) can be used as a name (aka key) for a mutex.
NPM package: [async-named-mutex](https://www.npmjs.com/package/async-named-mutex)