Skip to content

Instantly share code, notes, and snippets.

@mszkb
Created August 15, 2021 18:27
Show Gist options
  • Save mszkb/ec76c41081afed89d10e25ee2646d53f to your computer and use it in GitHub Desktop.
Save mszkb/ec76c41081afed89d10e25ee2646d53f to your computer and use it in GitHub Desktop.
axios - do 20 requests, but 6 at the same time (queue)
import axios from "axios";
import { TaskQueue } from 'cwait';
import "@babel/polyfill";
const MAX_SIMULTANEOUS_DOWNLOADS = 6;
const params = {
method: 'get',
url: 'http://localhost:3000/ping',
}
const urls = []
const amount = 20
for (let i = 0; i < amount; i++) {
urls.push('http://localhost:3000/ping/'+i)
}
const queue = new TaskQueue(Promise, MAX_SIMULTANEOUS_DOWNLOADS);
const results = Promise.all(urls.map(queue.wrap(async url => await axios.get(url).then(res => console.log(res.data)))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment