Zeit - a nice scheduler for Node.js

At one point or another you want to stuff in your code to happen at intervals. The requirements usually vary - it could be at fixed points in time, once every day, a fixed number of times, a regular interval between starts, a regular intervals between the last finish and the next start etc. Also - you could want logic statements to determine when to start or stop your schedules.

You could do all this with setTimeout and setInterval, but you could also do it with the npm module Zeit that abstracts all of the goriness away.

Super easy use case:

const zeit = require('zeit');

new zeit.Scheduler(new zeit.DateClock())
  .execute(myPromise)
  .atFixedIntervalOf(10000)
  .exactly(10)
  .start();

This runs myPromise every 10 seconds and stops after 10 times.

Variation:

const zeit = require('zeit');

new zeit.Scheduler(new zeit.DateClock())
  .after(3000)
  .execute(myPromise)
  .andRepeatAfter(10000)
  .until((err, result) => {
      return result === 'finished;
  })
  .start();

This waits for 3 seconds (after(3000)) then runs myPromise, and 10 seconds after myPromise resolves it runs myPromise again, until the return value of myPromise is 'finished' - then it stops the schedule.

The difference between atFixedIntervalOf and andRepeatAfter is that first does not wait for myPromise to resolve.

Read more about zeit and what it can do here.

comments powered by Disqus
Find me on Mastodon