Created
July 30, 2019 16:42
-
-
Save stursby/87dc753a73d49b0adb87ec9ba66d9652 to your computer and use it in GitHub Desktop.
Bottleneck example
This file contains hidden or 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
const axios = require('axios') | |
const Bottleneck = require('bottleneck') | |
const limiter = new Bottleneck({ | |
maxConcurrent: 1, | |
minTime: 5000 | |
}) | |
const urls = [ | |
'https://jsonplaceholder.typicode.com/users', | |
'https://jsonplaceholder.typicode.com/todos', | |
'https://jsonplaceholder.typicode.com/albums' | |
] | |
async function fetchData(url) { | |
console.log(`Fetching ${url}...`) | |
const { data } = await axios.get(url) | |
return data | |
} | |
async function init() { | |
for (url of urls) { | |
await limiter.schedule(() => fetchData(url)) | |
} | |
console.log('Done') | |
} | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment