Skip to content

Instantly share code, notes, and snippets.

@strax
Created December 8, 2015 21:27
Show Gist options
  • Save strax/a64205db464239a07b76 to your computer and use it in GitHub Desktop.
Save strax/a64205db464239a07b76 to your computer and use it in GitHub Desktop.
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