A simple flow framework by Promise.
It's time to say goodbye to async.waterfall
and async.parallel
.
If we want to implement the following flow:
Task1
return 'a';Task2_1
、Task2_2
、Task2_3
execute in parallel;Task2_1
receive 'a' return 'b1';Task2_2
receive 'a' return b2;Task3
receive 'b2' return 'c' afterTask2_2
excuted;
Task2_3
receive 'a' return 'b3';
Task4
receive ['b1', 'c', 'b3'].
Use async.waterfall
and async.parallel
:
async.waterfall([
function Task1(callback) {
callback(null, 'a');
},
function(last, callback) {
// last equals 'a'
async.parallel([
function Task2_1(callback) {
// last equals 'a'
callback(null, 'b1');
},
function(callback) {
async.waterfall([
function Task2_2(callback) {
// last equals 'a'
callback(null, 'b2');
},
function Task3(last, callback) {
// last equals 'b2'
callback(null, 'c');
}
], callback);
},
function Task2_3(callback) {
// last equals 'a'
callback(null, 'b3');
}
], callback);
},
function Task4(last, callback) {
// last equals ['b1', 'c', 'b3']
callback(null, 'd');
}
], function(err, result) {
// result equals 'd'
});
But with Flow
, it's simple and clean:
new Flow([
function Task1() {
return 'a';
},
[
function Task2_1(last) {
// last equals 'a'
return 'b1';
},
new Flow([
function Taks2_2(last) {
// last equals 'a'
return 'b2';
},
function Task3(last) {
// last equals 'b2'
return 'c';
}
]),
function Task2_3(last) {
// last equals 'a'
return 'b3';
}
],
function Task4(last) {
// last equals ['b1', 'c', 'b3']
console.log(last);
}
]).start();
Create a new Flow
instance.
Paramter tasks
is optional, it should be a task array if specified,
the task accept Function
and Flow
, or an Array
contains them.
Start the flow.
Paramter initValue
is optional, it's the init value to the fist task.
Append tasks to the flow instance.
Prepend tasks to the flaw instance.
You can extend the Flow
to do some interesting.
- clone this repo,
git clone https://github.com/iammapping/flow
- cd /the_flow_dir
npm install
typings install
- write with
TypeScript
MIT