Skip to content

Commit 4a2d323

Browse files
committed
Add waterfall flow
It has been tested using nodejs and iotjs. Tests have been also tested using NPM's async. Code originally published at: https://github.com/rzr/iotjs-async Change-Id: I9081b03bdec36b8a70cdef79cfacc0b67159e24e Origin: SamDelgado#1 Signed-off-by: Philippe Coval <philippe.coval@osg.samsung.com>
1 parent 45977c8 commit 4a2d323

File tree

2 files changed

+156
-1
lines changed

2 files changed

+156
-1
lines changed

async-lite.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,28 @@
175175
}
176176

177177
runTask(0);
178-
}
178+
},
179179

180+
// https://github.com/rzr/async-waterfall
181+
waterfall : function waterfall(tasks, callback /* ... */) {
182+
var results = Array.prototype.slice.call(arguments, 2);
183+
if (!tasks.length) {
184+
results.splice(0, 0, null); // insert "err" before "results"
185+
callback.apply(null, results);
186+
} else {
187+
var funct = tasks.shift();
188+
results.push(function(err,res) {
189+
if (err) {
190+
return callback(err,res);
191+
}
192+
var args = [tasks, callback];
193+
Array.prototype.push.apply(
194+
args, Array.prototype.slice.call(arguments, 1));
195+
waterfall.apply(null, args);
196+
});
197+
funct.apply(null, results);
198+
}
199+
}
180200
};
181201

182202
// Node.js

test/waterfall.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var async = require('../async-lite.js');
2+
var expect = require('expect.js');
3+
4+
describe('async.waterfall', function() {
5+
6+
it('should only process task arrays with at least 1 task', function(done) {
7+
8+
async.waterfall([], function(err, results) {
9+
10+
expect(err).to.not.be.ok();
11+
expect(results).to.not.be.ok();
12+
done();
13+
14+
});
15+
16+
});
17+
18+
it('should only process 1 task in the array at a time', function(done) {
19+
20+
var start_time = Date.now();
21+
22+
async.series([
23+
24+
function task1(callback) {
25+
setTimeout(function() {
26+
callback();
27+
}, 200);
28+
},
29+
30+
function task2(callback) {
31+
setTimeout(function() {
32+
callback();
33+
}, 400);
34+
},
35+
36+
function task3(callback) {
37+
setTimeout(function() {
38+
callback();
39+
}, 600);
40+
}
41+
42+
], function(err, results) {
43+
44+
var end_time = Date.now();
45+
var time_difference = end_time - start_time;
46+
47+
expect(err).to.not.be.ok();
48+
expect(results).to.be.an('array');
49+
expect(results[0]).to.not.be.ok();
50+
expect(results[1]).to.not.be.ok();
51+
expect(results[2]).to.not.be.ok();
52+
expect(time_difference).to.be.greaterThan(199);
53+
done();
54+
55+
});
56+
57+
});
58+
59+
it('should process each task in order', function(done) {
60+
61+
var start_time = Date.now();
62+
63+
async.waterfall([
64+
65+
function task1(callback) {
66+
setTimeout(function() {
67+
callback(null, ["task1"]);
68+
}, 600);
69+
},
70+
71+
function task2(results, callback) {
72+
setTimeout(function() {
73+
callback(null, results.concat("task2"));
74+
}, 400);
75+
},
76+
77+
function task3(results, callback) {
78+
setTimeout(function() {
79+
callback(null, results.concat("task3"));
80+
}, 200);
81+
}
82+
83+
], function(err, results) {
84+
85+
var end_time = Date.now();
86+
var time_difference = end_time - start_time;
87+
88+
expect(err).to.not.be.ok();
89+
expect(results).to.be.an('array');
90+
expect(results).to.eql(["task1", "task2", "task3"]);
91+
expect(time_difference).to.be.greaterThan(1200);
92+
done();
93+
94+
});
95+
96+
});
97+
98+
it('should stop processing the array of tasks when a task passes a truthy error value to the callback', function(done) {
99+
100+
var start_time = Date.now();
101+
102+
async.waterfall([
103+
104+
function task1(callback) {
105+
setTimeout(function() {
106+
callback(true, "task1");
107+
}, 600);
108+
},
109+
110+
function task2(callback) {
111+
setTimeout(function() {
112+
callback(null, "task2");
113+
}, 400);
114+
},
115+
116+
function task3(callback) {
117+
setTimeout(function() {
118+
callback(null, "task3");
119+
}, 200);
120+
}
121+
122+
], function(err, results) {
123+
124+
var end_time = Date.now();
125+
var time_difference = end_time - start_time;
126+
127+
expect(err).to.be.ok();
128+
expect(results).to.be.ok();
129+
expect(time_difference).to.be.lessThan(1000);
130+
done();
131+
132+
});
133+
});
134+
135+
});

0 commit comments

Comments
 (0)