https://github.com/expo/await-lock
Mutex locks for async functions
https://github.com/expo/await-lock
async-await concurrency mutex-lock
Last synced: 3 months ago
JSON representation
Mutex locks for async functions
- Host: GitHub
- URL: https://github.com/expo/await-lock
- Owner: expo
- License: mit
- Created: 2015-02-06T05:16:06.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2025-02-24T07:46:55.000Z (4 months ago)
- Last Synced: 2025-03-30T08:09:33.628Z (3 months ago)
- Topics: async-await, concurrency, mutex-lock
- Language: TypeScript
- Homepage:
- Size: 974 KB
- Stars: 93
- Watchers: 12
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AwaitLock  [](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
This package is published only as an ES module. In addition to importing ES modules from ES modules, modern versions of Node.js support [requiring ES modules from CommonJS modules](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require).
```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();
}
});
```