Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gruhn/callforth
Don't callback, callforth! :running:
https://github.com/gruhn/callforth
async-await callback javascript promise
Last synced: 8 days ago
JSON representation
Don't callback, callforth! :running:
- Host: GitHub
- URL: https://github.com/gruhn/callforth
- Owner: gruhn
- Created: 2019-08-23T18:35:23.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T15:10:59.000Z (almost 2 years ago)
- Last Synced: 2024-12-12T22:43:55.636Z (14 days ago)
- Topics: async-await, callback, javascript, promise
- Language: JavaScript
- Homepage:
- Size: 397 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Callforth :running:
[![npm version](https://badge.fury.io/js/callforth.svg)](https://badge.fury.io/js/callforth)
![minzipped size](https://badgen.net/bundlephobia/minzip/callforth)A tiny utility library to replace callbacks with Promises where possible.
**Don't callback, callforth!**It simply includes a hand-full of functions I see myself re-implementing in nearly every project.
So I might as well put them in a package.Do things like:
```js
await timeout(3000)
```
```js
await eventOn(videoElement, "loadeddata")
```
```js
const message = await eventOn(webWorker, "message")
```## Install :package:
```sh
npm install callforth
```Now you can:
```js
import { eventOn, timeout, polling } from "callforth"
```Alternatively, include [this script](https://unpkg.com/callforth/dist/callforth.umd.js) and:
```html
const { eventOn, timeout, polling } = window.callforth
```
## API :eyes:
### `eventOn`
```js
const payload = await eventOn(target, successEvent, errorEvent)
```#### Parameters
* _target_ : `EventTarget` - any object you can call `addEventListener` on.
* _successEvent_ : `string` - name of the event you want to await.
* _errorEvent_ : `string` (optional) - if this event fires, the promise is rejected.#### Return Value
* `Promise` - wraps callback result (callbacks first argument)
### `timeout`
```js
await timeout(delay)
```#### Parameters
* _delay_ : `int` - milliseconds after which the Promise should resolves.
#### Return Value
* `Promise`
### `polling`
```js
await polling(predicate, { maxTries, interval })
```#### Parameters
* _predicate_ : `any -> boolean` - delay in milliseconds after which the Promise should resolve.
* _options_ : `object` (optional)
* _maxTries_ : `int` (default = 10) - maximum number of times to call predicate before giving up.
* _interval_ : `int` (default = 10) - delay in milliseconds between calls of predicate.#### Return Value
* `Promise`
## More Examples
```js
async function loadScript(url) {
let script = document.createElement("script")script.src = url
await eventOn(script, "loaded")
}
```
```js
async function primesLessThen(number) {
primeWorker.postMessage(number)const result = await eventOn(primeWorker, "message")
return result
}
```