Name: piggybacker
Owner: TABLEFLIP
Description: Async keyed job runner, piggyback on results from running jobs with the same key
Created: 2018-04-05 21:08:22.0
Updated: 2018-04-06 08:49:13.0
Pushed: 2018-04-06 08:49:10.0
Homepage: null
Size: 196
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Async keyed job runner, piggyback on results from running jobs with the same key
rt { piggyback } from 'piggybacker'
c function fetchJSON (url) {
nst res = await window.fetch(url)
turn res.json()
t piggyFetchJSON = piggyback(
tchJSON,
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.
nction getKey (url) {
return url
t 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.
ggyFetchJSON('https://example.org/data.json'),
ggyFetchJSON('https://example.org/data.json'),
ggyFetchJSON('https://example.org/data.json')
etchJSON called only ONCE!
piggyback(fn, getKey)
Create a new function that'll call fn
and piggyback on the results if called again.
fn
- the function to piggyback ongetKey
- 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 piggybackedFeel free to dive in! Open an issue or submit PRs.
MIT © Alan Shaw