1

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!

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
Sergey Potekhin
  • 621
  • 1
  • 11
  • 27

1 Answers1

1

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

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43