Last active
August 29, 2015 14:17
-
-
Save sillero/cc4949b4300647d2dfe5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment