-
-
Save strax/a64205db464239a07b76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Bacon from 'baconjs'; | |
import fetch from 'node-fetch'; | |
const makeRequest = () => fetch('https://wtfismyip.com/text'); | |
const createThrottler = (onValue, maxConcurrencyInWindow = 5, windowLength = 5000) => { | |
const bus = new Bacon.Bus(); | |
bus.flatMapWithConcurrencyLimit(maxConcurrencyInWindow, fn => ( | |
Bacon.fromPromise(fn()).concat(Bacon.later(windowLength).filter(false)) | |
)).onValue(onValue); | |
return fn => bus.push(fn); | |
} | |
const throttle = createThrottler(res => res.text().then(text => console.log(text.trim()))); | |
Array(30).fill(() => makeRequest()).forEach(fn => throttle(fn)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment