How to Add Timer in Node js Code?


Adding timer In Node Js Example Code




var array = ['some', 'array', 'containing', 'words'];
var interval = 1000; // how much time should the delay between two iterations be (in milliseconds)?
var promise = Promise.resolve();
array.forEach(function (el) {
  promise = promise.then(function () {
    console.log(el);
    return new Promise(function (resolve) {
      setTimeout(resolve, interval);
    });
  });
});


promise.then(function () {
  console.log('Loop finished.');
});

Comments

Popular posts from this blog

What is HTML? Complete Beginner’s Guide (with Examples)

HTML Forms — Input, Label, Button & Validation (Explained Like a Teacher With Examples)

Difference Between HTML and HTML5 (2025 Explained) | Features, Advantages & Comparison Table