This algorithm does heavy use of arrow functions. Use Babel if you need wider support.
var pubsub = require('pub-sub');
pubsub.sub('channel', function (message) {
if (message === 'close') { // Unsubscribe when 'close' is received'
console.log('Shuting down...');
return false;
} else {
console.log(message);
return true;
}
});
var countdown = 10;
var interval = setInterval(function () {
if (--countdown) {
pubsub.pub('channel', 'Seconds until shutdown: ' + countdown);
} else {
pubsub.pub('channel', 'close');
clearInterval(interval);
}
}, 1000);
Heavily inspired by this