Skip to content

Iterate Asynchronously in Sequence

js
const tasks = []; // array of async tasks

function iterate(index) {
  if (index === tasks.length) {
    return finish();
  }

  const task = tasks[index];
  task(function () {
    // async callback: will execute when async task completes
    iterate(index + 1);
  });
}

function finish() {
  // iteration completed
}

iterate(0); // initiate series of async task execution