https://github.com/tableflip/piggybacker
Async keyed job runner, piggyback on results from running jobs with the same key
https://github.com/tableflip/piggybacker
Last synced: about 1 year ago
JSON representation
Async keyed job runner, piggyback on results from running jobs with the same key
- Host: GitHub
- URL: https://github.com/tableflip/piggybacker
- Owner: tableflip
- License: mit
- Created: 2018-04-05T21:08:22.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-06T08:49:10.000Z (about 8 years ago)
- Last Synced: 2024-12-21T12:36:47.387Z (over 1 year ago)
- Language: JavaScript
- Size: 191 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# piggybacker
[](https://travis-ci.org/tableflip/piggybacker) [](https://david-dm.org/tableflip/piggybacker) [](https://standardjs.com)
> Async keyed job runner, piggyback on results from running jobs with the same key
## Usage
```js
import { piggyback } from 'piggybacker'
async function fetchJSON (url) {
const res = await window.fetch(url)
return res.json()
}
const piggyFetchJSON = piggyback(
fetchJSON,
// Given the args that will be passed to fetchJSON, generate a key so that if
// a second call is made while the first is in flight then the second will
// ALSO receive the results of the first, instead of having to make a
// separate request.
function getKey (url) {
return url
}
)
const results = await Promise.all([
// Multiple calls to piggyFetchJSON with the same URL _while_ a request is in
// progress will not send another request! Instead they'll wait on the results
// of the first.
piggyFetchJSON('https://example.org/data.json'),
piggyFetchJSON('https://example.org/data.json'),
piggyFetchJSON('https://example.org/data.json')
])
// fetchJSON called only ONCE!
```
## API
### `piggyback(fn, getKey)`
Create a new function that'll call `fn` and piggyback on the results if called again.
* `fn` - the function to piggyback on
* `getKey` - a function called before each call to `fn` that generates a key for the call. Two or more calls with the same key will be piggybacked
## Contribute
Feel free to dive in! [Open an issue](https://github.com/tableflip/piggybacker/issues/new) or submit PRs.
## License
[MIT](LICENSE) © Alan Shaw