Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ide/await-lock
Mutex locks for async functions
https://github.com/ide/await-lock
async-await concurrency mutex-lock
Last synced: about 22 hours ago
JSON representation
Mutex locks for async functions
- Host: GitHub
- URL: https://github.com/ide/await-lock
- Owner: ide
- License: mit
- Created: 2015-02-06T05:16:06.000Z (almost 10 years ago)
- Default Branch: main
- Last Pushed: 2024-11-18T22:41:52.000Z (about 1 month ago)
- Last Synced: 2024-12-14T14:06:01.621Z (8 days ago)
- Topics: async-await, concurrency, mutex-lock
- Language: TypeScript
- Homepage:
- Size: 1000 KB
- Stars: 91
- Watchers: 4
- Forks: 12
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AwaitLock ![tests](https://github.com/ide/await-lock/workflows/Tests/badge.svg) [![codecov](https://codecov.io/gh/ide/await-lock/branch/master/graph/badge.svg)](https://codecov.io/gh/ide/await-lock)
Mutex locks for async functions# API
[API documentation](https://github.com/ide/await-lock/wiki/API-documentation)
# Usage
```javascript
import AwaitLock from 'await-lock';let lock = new AwaitLock();
async function runSerialTaskAsync() {
await lock.acquireAsync();
try {
// IMPORTANT: Do not return a promise from here because the finally clause
// may run before the promise settles, and the catch clause will not run if
// the promise is rejected
} finally {
lock.release();
}
}
```You can also use AwaitLock with [co](https://github.com/tj/co) and generator functions.
```javascript
import AwaitLock from 'await-lock';let runSerialTaskAsync = co.wrap(function*() {
yield lock.acquireAsync();
try {
// Run async code in the critical section
} finally {
lock.release();
}
});
```