Skip to content

Instantly share code, notes, and snippets.

@joedotjs
Created June 29, 2015 16:51
Show Gist options
  • Save joedotjs/d40175cc8f72c81a5e4c to your computer and use it in GitHub Desktop.
Save joedotjs/d40175cc8f72c81a5e4c to your computer and use it in GitHub Desktop.
var Promise = require('bluebird');
var pipeline = function (tasksThatReturnPromises, prevValue) {
var first = tasksThatReturnPromises.shift();
return Promise.resolve(first(prevValue)).then(function (v) {
if (tasksThatReturnPromises.length === 0) {
return v;
} else {
return pipeline(tasksThatReturnPromises, v);
}
});
};
var fs = Promise.promisifyAll(require('fs'));
pipeline([
function () {
return fs.readFileAsync('./pipe.js');
},
function (contents) {
return contents.toString();
},
function (stringedContents) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve('LOL' + stringedContents + 'LOL');
}, 3000);
});
}
]).then(function (result) {
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment