Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stagas/atomic
makes a function atomic with timeout and abort signal
https://github.com/stagas/atomic
abort atomic concurrency promise race-conditions signal timeout
Last synced: 13 days ago
JSON representation
makes a function atomic with timeout and abort signal
- Host: GitHub
- URL: https://github.com/stagas/atomic
- Owner: stagas
- Created: 2011-10-10T16:13:02.000Z (about 13 years ago)
- Default Branch: main
- Last Pushed: 2022-01-20T06:36:42.000Z (almost 3 years ago)
- Last Synced: 2024-07-11T00:00:03.839Z (4 months ago)
- Topics: abort, atomic, concurrency, promise, race-conditions, signal, timeout
- Language: TypeScript
- Homepage:
- Size: 10.7 KB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
atomic
makes a function atomic with timeout and abort signal
🔧 Install
· 🧩 Example
· 📜 API docs
· 🔥 Releases
· 💪🏼 Contribute
· 🖐️ Help***
## Install
```sh
$ npm i atomic
```## API
#### Table of Contents
* [atomic](#atomic)
* [Parameters](#parameters)### atomic
[src/index.ts:33-78](https://github.com/stagas/atomic/blob/2a0a9f5694e915eb4fe86dea4895ed398d1458c3/src/index.ts#L33-L78 "Source code on GitHub")
Makes a function atomic.
```js
const fn = atomic(signal => async () => {
// some long async operation// if we've been aborted during the long
// async process above, we wouldn't want
// to continue so lets return here
if (signal.aborted) return// do things here if we didn't abort
}, 500) // timeout at 500ms (don't pass anything for no timeout)
fn()
fn()
fn()
await fn() // this will run after the above have settled
```#### Parameters
* `signalClosure` **function (signal: AbortSignal): function (...args: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\): [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\** A function that receives the signal object
from an AbortController and returns the function to become atomic.
* `maxTimeMs` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Time in milliseconds to timeout the operation. Will also signal abort.Returns **any** An atomic function
## Contribute
[Fork](https://github.com/stagas/atomic/fork) or
[edit](https://github.dev/stagas/atomic) and submit a PR.All contributions are welcome!
## License
MIT © 2022
[stagas](https://github.com/stagas)