Created
December 23, 2013 19:30
Example of async.waterfall, see http://stackoverflow.com/questions/20749359/node-async-waterfall-using-an-array-with-callbacks-that-have-arguments
This file contains hidden or 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
~/tmp ❯❯❯ node index.js | |
null | |
[ {}, {}, {} ] |
This file contains hidden or 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 async = require('async'); | |
var FabricCommand = function() {}; | |
FabricCommand.prototype.do = function (undoArray, callback) { | |
// Check the number of arguments | |
// (the first function of the waterfall receives only a callback) | |
if (Array.prototype.slice.apply(arguments).length === 1) { | |
callback = undoArray; | |
undoArray = undefined; | |
} | |
var self = this; | |
if (undoArray === undefined) { | |
undoArray = []; | |
} | |
undoArray.push(self); | |
callback(null, undoArray); | |
}; | |
var doCommands = []; | |
var f1 = new FabricCommand(); | |
var f2 = new FabricCommand(); | |
var f3 = new FabricCommand(); | |
doCommands.push(f1.do.bind(f1)); | |
doCommands.push(f2.do.bind(f2)); | |
doCommands.push(f3.do.bind(f3)); | |
async.waterfall( | |
doCommands, | |
function (err, undoCommands) { | |
console.log(err); | |
console.log(undoCommands); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment