Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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)