Experiment of the day. Using generators to create a pipeline for simple tasks.

https://gist.github.com/sillero/cc4949b4300647d2dfe5


// using iojs-v1.5.1 to run the code

/* jshint esnext:true */
/* globals console */
'use strict';
var app = {
  pipeline: function(){
    var pipeline = {};

    // using arguments slice because iojs needs flags for Spreads
    pipeline.tasks = Array.prototype.slice.call(arguments);

    pipeline.generator = function*(){
      for (let k = 0; k < pipeline.tasks.length; k++) {
        yield pipeline.tasks[k](pipeline.iterator.next.bind(pipeline.iterator));
      }
    };

    pipeline.iterator = pipeline.generator();

    return {
      pipeline: pipeline,
      start: pipeline.iterator.next.bind(pipeline.iterator)
    };
  }
};
var tasks = app.pipeline(task1, task2, task3);

tasks.start();

// the tasks

function task1(done){
  setImmediate(function(){
    console.log('task1 done!');
    done();
  });
}

function task2(done){
  setTimeout(function(){
    console.log('task2 done!');
    done();
  }, 3000);
}

function task3(done){
  setImmediate(function(){
    console.log('task3 done!');
    done();
  });
}