Skip to content

Instantly share code, notes, and snippets.

@guillaume
Created February 1, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaume/dfdf6ead0479d3a45e4ada64758db188 to your computer and use it in GitHub Desktop.
Save guillaume/dfdf6ead0479d3a45e4ada64758db188 to your computer and use it in GitHub Desktop.
import fetchPool from "./fetchPool";
const fetch = fetchPool(5);
// const urlfoo = ...fill in the void...
// will never fetch more than 5 at a time
for (var i = 0; i < 100; i++) {
fetch(urlfoo);
}
import Semaphore from "semaphore-async-await";
const fetchPool = poolCount => {
const lock = new Semaphore(poolCount);
return async function() {
await lock.wait();
const res = await fetch.apply(this, arguments);
lock.signal();
return res;
};
};
export default fetchPool;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment