Skip to content

Instantly share code, notes, and snippets.

@stigok
Created March 10, 2016 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stigok/8457d1e61431369916b6 to your computer and use it in GitHub Desktop.
Save stigok/8457d1e61431369916b6 to your computer and use it in GitHub Desktop.
Node.js Task & Worker
'use strict';
const _ = require('underscore');
function Task(fn) {
this.fn = fn;
}
function Worker(interval, tasks) {
this.interval = interval;
this.tasks = tasks || [];
this._timers = [];
this.start = tick => {
let tasks = _.isFunction(tick) ? [tick, ...this.tasks] : this.tasks;
this._timers = _.map(tasks, task => setInterval(task, this.interval));
};
this.stop = () => {
_.each(this._timers, timer => clearInterval(timer));
};
}
module.exports = {
Task: Task,
Worker: Worker
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment