Click here to Skip to main content
15,798,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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', /* and so on ... */];
const rateLimiter = new RateLimiter({ limit: 2 });
const promises = => () => {
        console.log(`Getting user data of ${user}...`);
        const userData = await getHTTP(`${user}`);
        console.log(`Got user data of ${user}.`);
        return userData;
const results = await Promise.all(promises);
console.log(results); // [data of user_a, user_b, user_c, ...]

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.
Updated 15-Sep-20 5:54am
Gerry Schmitz 15-Sep-20 10:11am    
You need a task to monitor a queue of tasks so that only 2 are ever dispatched / running at the same time. "RateLimiter" sounds like baggage.
hmdismail 15-Sep-20 10:18am    
do you have any ideas not to use JS async instead of actual threads? example could workerpool work?

1 solution

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900