https://github.com/prodo-dev/js-async
JavaScript utility library for using async/await
https://github.com/prodo-dev/js-async
Last synced: 11 months ago
JSON representation
JavaScript utility library for using async/await
- Host: GitHub
- URL: https://github.com/prodo-dev/js-async
- Owner: prodo-dev
- Created: 2018-04-27T17:07:42.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-06T20:15:28.000Z (over 7 years ago)
- Last Synced: 2024-04-26T06:21:01.776Z (almost 2 years ago)
- Language: JavaScript
- Size: 41 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JS ASync
Utility library for handling async/await.
## Usage
### denodeify
Turns a function that accepts a node-style callback into one that returns a `Promise`:
```javascript
import {denodeify} from "@prodo-ai/js-async";
const nodeStyleFunction = (arg1, arg2, callback) => {
if (arg1 == null) {
callback(new Error("Must specify `arg1`."));
}
callback(null, arg1 + arg2);
}
const asyncFunction = denodeify(nodeStyleFunction);
asyncFunction(1, 2)
.then(console.log)
.catch(console.error);
```
### fromCallback
The same as `denodeify`, but without error handling. Probably don't use this.
### keepTrying
Keeps attempting a behaviour until it succeeds:
```javascript
import {keepTrying} from "@prodo-ai/js-async";
const result = await keepTrying({
behaviour: () => service.getResult()
});
```
You can also pass additional parameters for greater control:
* `until` - a predicate that can be used to determine success. By default, this predicate just checks if the result is non-null.
* `ignoreErrors` - if `false`, gives up trying after an error is thrown.
* `timeout` - a timeout, after which an `Error` is thrown.
* `inbetweenAttempts` - a callback that is executed between each attempt.
### waitUntil
Waits until a predicate passes:
```javascript
import {waitUntil} from "@prodo-ai/js-async";
import {duration, SECOND} from "@prodo-ai/js-timing";
waitUntil({
condition: async () => (await service.getStatus()) === "OK",
pauseTime: duration(1, SECOND),
}).then(service.getResult);
```
You can also pass additional parameters for greater control:
* `timeout` - a timeout, after which an `Error` is thrown.
## Information
Owner: Prodo Tech Ltd
Maintainer: [tdawes](https://github.com/tdawes)
License: UNLICENSED (for now)