Skip to content

Add waterfall flow #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion async-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,28 @@
}

runTask(0);
}
},

// https://github.com/rzr/async-waterfall
waterfall : function waterfall(tasks, callback /* ... */) {
var results = Array.prototype.slice.call(arguments, 2);
if (!tasks.length) {
results.splice(0, 0, null); // insert "err" before "results"
callback.apply(null, results);
} else {
var funct = tasks.shift();
results.push(function(err,res) {
if (err) {
return callback(err,res);
}
var args = [tasks, callback];
Array.prototype.push.apply(
args, Array.prototype.slice.call(arguments, 1));
waterfall.apply(null, args);
});
funct.apply(null, results);
}
}
};

// Node.js
Expand Down
135 changes: 135 additions & 0 deletions test/waterfall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
var async = require('../async-lite.js');
var expect = require('expect.js');

describe('async.waterfall', function() {

it('should only process task arrays with at least 1 task', function(done) {

async.waterfall([], function(err, results) {

expect(err).to.not.be.ok();
expect(results).to.not.be.ok();
done();

});

});

it('should only process 1 task in the array at a time', function(done) {

var start_time = Date.now();

async.series([

function task1(callback) {
setTimeout(function() {
callback();
}, 200);
},

function task2(callback) {
setTimeout(function() {
callback();
}, 400);
},

function task3(callback) {
setTimeout(function() {
callback();
}, 600);
}

], function(err, results) {

var end_time = Date.now();
var time_difference = end_time - start_time;

expect(err).to.not.be.ok();
expect(results).to.be.an('array');
expect(results[0]).to.not.be.ok();
expect(results[1]).to.not.be.ok();
expect(results[2]).to.not.be.ok();
expect(time_difference).to.be.greaterThan(199);
done();

});

});

it('should process each task in order', function(done) {

var start_time = Date.now();

async.waterfall([

function task1(callback) {
setTimeout(function() {
callback(null, ["task1"]);
}, 600);
},

function task2(results, callback) {
setTimeout(function() {
callback(null, results.concat("task2"));
}, 400);
},

function task3(results, callback) {
setTimeout(function() {
callback(null, results.concat("task3"));
}, 200);
}

], function(err, results) {

var end_time = Date.now();
var time_difference = end_time - start_time;

expect(err).to.not.be.ok();
expect(results).to.be.an('array');
expect(results).to.eql(["task1", "task2", "task3"]);
expect(time_difference).to.be.greaterThan(1200);
done();

});

});

it('should stop processing the array of tasks when a task passes a truthy error value to the callback', function(done) {

var start_time = Date.now();

async.waterfall([

function task1(callback) {
setTimeout(function() {
callback(true, "task1");
}, 600);
},

function task2(callback) {
setTimeout(function() {
callback(null, "task2");
}, 400);
},

function task3(callback) {
setTimeout(function() {
callback(null, "task3");
}, 200);
}

], function(err, results) {

var end_time = Date.now();
var time_difference = end_time - start_time;

expect(err).to.be.ok();
expect(results).to.be.ok();
expect(time_difference).to.be.lessThan(1000);
done();

});
});

});