NodeJS – Looping through Array Sequentially with Timeout between each Element in Array

I have a list of commands in an array that I need to run in order:

const commands = [
  `git clone https://github.com/EliLillyCo/${repo}.git`,
  `cd ${repo}`, `git checkout -b ${branch}`,
  'cp ../codeql-analysis.yml .github/workflows/',
  'git add .github/workflows/codeql-analysis.yml',
  `git push --set-upstream origin ${branch}`,
  'cd ../',
  `rm -r  ${repo}`,
];

They need to be ran in order as the commands rely on the previous command being ran.

Also, each command needs to have a 3 second wait before running the next command, because sometimes commands take time, especially command 1 and command 5.

I am using a standard for loop which is then using setTimeout() that calls a function to run the commands, as such:


const a = require('debug')('worker:sucess');
const b = require('debug')('worker:error');

const { exec } = require('child_process');

function execCommand(command) {
  exec(command, (error, stdout, stderr) => {
    if (error) {
      b(`exec error: ${error}`);
      return;
    }
    a(`stdout: ${stdout}`);
    b(`stderr: ${stderr}`);
  });
}

const commands = [
   `git clone https://github.com/EliLillyCo/${repo}.git`,
   `cd ${repo}`, `git checkout -b ${branch}`,
   'cp ../codeql-analysis.yml .github/workflows/',
   'git add .github/workflows/codeql-analysis.yml',
   `git push --set-upstream origin ${branch}`,
   'cd ../',
   `rm -r  ${repo}`,
 ];

for (let i = 0; i < commands.length; i++) {
  setTimeout(execCommand(commands[i]), 3000);
}

But there is something wrong with the setTimeout() as it’s returning this:

  worker:error TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined

What is the best way to approach the problem of looping through an array sequentially, whilst using a timeout?

165 thoughts on “NodeJS – Looping through Array Sequentially with Timeout between each Element in Array”

  1. Nice post. I was checking continuously this blog and I’m impressed!
    Very useful information specifically the last part 🙂 I care
    for such information much. I was seeking this certain info for a
    long time. Thank you and best of luck.

    Reply
  2. I’m truly enjoying the design and layout of your site.
    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often.
    Did you hire out a developer to create your theme? Great work!

    Reply
  3. That is very fascinating, You’re an excessively professional
    blogger. I’ve joined your rss feed and look ahead to
    in search of more of your excellent post. Also, I have shared
    your web site in my social networks

    Reply
  4. Hey just wanted to give you a quick heads up and let you know a few of the images aren’t loading correctly.
    I’m not sure why but I think its a linking issue. I’ve tried it in two different web browsers and both
    show the same results.

    Reply
  5. It is appropriate time to make a few plans for the longer term and it
    is time to be happy. I’ve learn this post and if I may just I desire to suggest you few attention-grabbing issues or advice.
    Perhaps you could write next articles relating to this article.
    I desire to learn even more things about it!
    scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery

    Reply
  6. Pingback: aralen cost uk
  7. Pingback: molnupiravir 400
  8. Pingback: is latisse safe
  9. Pingback: zanaflex drug
  10. Pingback: order molnupiravir