Created
February 27, 2012 08:19
-
-
Save nakamura-to/1922527 to your computer and use it in GitHub Desktop.
experimental implementation: flow.exec
This file contains 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
var flow = require('../index').flow; | |
var fs = require('fs'); | |
var hogeFlow = flow( | |
function (a, b) { | |
var self = this; | |
throw new Error('ERROR'); | |
setTimeout(function () { | |
self.next('HELLO' + a + b); | |
}, 0); | |
} | |
); | |
flow.exec = function exec(fn) { | |
var callback = arguments[arguments.length - 1]; | |
var args = Array.prototype.slice.call(arguments, 1, arguments.length - 1); | |
flow(fn, function () { | |
callback.apply(null, [this.err].concat(this.args)); | |
}).apply(null, args); | |
} | |
var myFlow = flow( | |
function readFiles(file1, file2) { | |
fs.readFile(file1, 'utf8', this.async()); | |
fs.readFile(file2, 'utf8', this.async()); | |
flow.exec(hogeFlow, 'a', 'b', this.async()); | |
}, | |
function concat(data1, data2, data3) { | |
this.next(data1 + data2 + data3); | |
}, | |
function end(data) { | |
if (this.err) throw this.err; | |
console.log(data); | |
console.log('done'); | |
this.next(); | |
} | |
); | |
myFlow('file1', 'file2'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment