https://github.com/oliver-oloughlin/async_lock
Simple and small-weight async lock for running callback functions sequentially, with zero dependencies.
https://github.com/oliver-oloughlin/async_lock
async-lock deno javascript jsr lock typescript
Last synced: about 2 months ago
JSON representation
Simple and small-weight async lock for running callback functions sequentially, with zero dependencies.
- Host: GitHub
- URL: https://github.com/oliver-oloughlin/async_lock
- Owner: oliver-oloughlin
- License: mit
- Created: 2025-01-26T21:15:57.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-16T13:34:33.000Z (over 1 year ago)
- Last Synced: 2025-02-16T14:42:25.883Z (over 1 year ago)
- Topics: async-lock, deno, javascript, jsr, lock, typescript
- Language: TypeScript
- Homepage: https://jsr.io/@olli/async-lock
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# async_lock
Simple and small-weight async lock for running callback functions sequentially,
with zero dependencies.
## Run
Sequentially run tasks using a queue based strategy. New tasks are queued and
wait for prior tasks to complete.
```ts
import { AsyncLock } from "@olli/async-lock";
const lock = new AsyncLock();
async function critical() {
// ...critical logic that needs to be run one instance at a time
}
// All calls of `critical()` are run sequentially, no matter the order
await Promise.all([
lock.run(() => critical()),
lock.run(() => critical()),
lock.run(() => critical()),
]);
```
## Cancel
Cancel and remove any queued tasks.
```ts
import { AsyncLock } from "@olli/async-lock";
const lock = new AsyncLock();
async function critical() {
// ...critical logic that needs to be run one instance at a time
}
Promise.all([
lock.run(() => critical()), // Not cancelled, as the first task does not need to wait
lock.run(() => critical()), // Cancelled
lock.run(() => critical()), // Cancelled
]);
lock.cancel();
```
## Development
Any contributions are welcomed and appreciated. How to contribute:
- Clone this repository
- Add feature / Refactor
- Add or refactor tests as needed
- Ensure code quality (check + lint + format + test) using `deno task prep`
- Open Pull Request
## License
Published under
[MIT License](https://github.com/oliver-oloughlin/async_lock/blob/main/LICENSE)