I wanted to design and implement a RateLimiter function, which allows the following usage:
from my understanding, when somebody calls do, the RateLimiter needs to do one of two things with the passed-in lambda:
-Start executing it immediately-Queue it up for later execution
In my case, if i call dothree times in rapid succession, the first two async functions should be immediately started while the third should be queued up.
Because the rate limiter needs to monitor the state of any async functions that it starts, it needs to install a callback on the returned Promise.
So there are essentially two "events" that the rate limiter is concerned with:
-Somebody calls do
-One of the spawned tasks completes (either successfully or unsuccessfully)
can anyone try to implement it?
const userList = ['user_a', 'user_b', 'user_c', ];
const rateLimiter = new RateLimiter({ limit: 2 });
const promises = userList.map((user) =>
rateLimiter.do(async () => {
console.log(`Getting user data of ${user}...`);
const userData = await getHTTP(`https:
console.log(`Got user data of ${user}.`);
return userData;
})
);
const results = await Promise.all(promises);
console.log(results);
What I have tried:
I think its easiest to use Promise.finally to do that.
The finally callback will run whether the promise is completed or rejected, so it'll handle both success and failure cases.
When any in-flight async function finishes and my installed Promise.finally handler is called, i will need to determine if there are any queued async functions. If so, i will need to start one.