Sometimes the server may have a limitation to respond to the number of API calls at the same moment. Making 100s of concurrent calls to a single server will have an impact & it may be assumed as DOS attack.
This can be handled if we could throttle the API calls during implementation.
While throttling the API calls can still be handled from the developer's perspective, however proper checks needs to be done at load balancer or proxy to avoid any type of DOS attacks.
While there are a lot of fantastic npm modules available, I thought of doing a short POC and write a simple one for my own understanding using some generators.
Let me know in the comments if this is the proper approach :)
const fetch = require("isomorphic-fetch");
const totalPromiseLength = 5;
const requestMethod = url => () => fetch(url).then(response => response.json());
let promiseArray = [...new Array(totalPromiseLength).keys()].map(index =>
requestMethod("https://jsonplaceholder.typicode.com/todos/" + (index + 1))
);
function* chunks(arr, limit) {
for (let i = 0; i < Math.ceil(arr.length / limit); ++i) {
console.log("requested")
yield [...arr].slice(i * limit, i * limit + limit);
}
}
new Promise(async resolve => {
let generated = chunks(promiseArray, 2);
let result = [];
for (let bla of generated) {
await Promise.all(bla.map(param => param())).then(response => {
console.log('resolved')
result = [...result, ...response];
if (result.length === promiseArray.length) {
resolve(result);
}
});
}
}).then(response => {
console.log(response);
});
Top comments (0)