Languages
[Edit]
EN

JavaScript - how to make buffered async loop class with next iteration confirmation?

4 points
Created by:
Creg
9600

In this article, we're going to have a look at how to write own asynchronous buffered loop class  (window loop class) that uses callback functions to execute next iterations in proper time or breaks loop - processes asynchronous tasks as resources are available.

This kind of loop is useful when we have to make some iterations and shedule them in groups, e.g. optimisation that has to execute 50 computations but available are only 5 machines and we don't want to make new interations until machine is avaialble.

Note: read this article to see simple async loop with next iteration confirmation example.

1. Custom class example

Edit

This implementation uses setTimeout function to call iterations in proper time. To be sure that next iteration will be executed it is necessary to call resume() method in each onIteration method. To break loop it is just necessary to call finish() method.

Note: in below example we shedule next iterations after 1s and break loop after 10 iterations to show how it works.

 

Alternative titles

  1. JavaScript - async loop with window
  2. JavaScript - process asynchronous tasks as resources are available
  3. JavaScript - async loop with many iterations in same time
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join