Rate Limiter Problem
You are given an array of URLs: ['url1', 'url2', ...]
and a limit on the number of simultaneous requests - limit
.
Implement a function that queries the URLs and invokes a callback with an array of responses: ['url1_answer', 'url2_answer', ...]
such that at any given time, no more than limit requests are executed concurrently. As soon as one request completes, the next one should be initiated.
In other words, you need to implement a request queue with
a width equal to limit
.
Requirements:
- The order of the responses in the result array should match the order of the URLs in the input array
- The function should have memoization to avoid querying the same URL more than once
- You can use
fetch
for the requests - There is no need to handle errors
function parallelLimit(urls, limit, callback) {
// your code here
}