Created
February 1, 2018 19:26
-
-
Save guillaume/dfdf6ead0479d3a45e4ada64758db188 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 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); | |
} |
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 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