Javascript - in-process function executor with rate limiter?

Imagine that I have some function f() which should be executed constantly. But I want to set a rate limit for execution, something like "Execute function no more than 10 times in 1 minute". In a code view I think it can have the following interface:

(async () => {
  const f = async () => {};
  const executor = new Executor({ max: 1, duration: 10000 });

  Array(10).fill(null).map(x => executor.addJob(f));

  await executor.execute();
})();

I have a few ideas about how to implement this on pure JS, but some implementations are already available? Also, I know that it's possible to implement with some 3-party soft, such as Bull queue and so on, but I need exactly in-process implementation.

Thanks in advance!

Answers:

Answer

What are you seeking for is known as throttling. You can find it implemented in popular libraries like lodash's throttle or you can implement it in vanilla javascript like in this answer

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.