diff options
Diffstat (limited to 'lib/waterfall.js')
-rw-r--r-- | lib/waterfall.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/waterfall.js b/lib/waterfall.js new file mode 100644 index 0000000..8867c87 --- /dev/null +++ b/lib/waterfall.js @@ -0,0 +1,32 @@ +'use strict'; + +import isArray from 'lodash/isArray'; +import noop from 'lodash/noop'; +import once from 'lodash/once'; +import rest from 'lodash/rest'; + +import ensureAsync from './ensureAsync'; +import iterator from './iterator'; + +export default function(tasks, cb) { + cb = once(cb || noop); + if (!isArray(tasks)) return cb(new Error('First argument to waterfall must be an array of functions')); + if (!tasks.length) return cb(); + + function wrapIterator(iterator) { + return rest(function(err, args) { + if (err) { + cb.apply(null, [err].concat(args)); + } else { + var next = iterator.next(); + if (next) { + args.push(wrapIterator(next)); + } else { + args.push(cb); + } + ensureAsync(iterator).apply(null, args); + } + }); + } + wrapIterator(iterator(tasks))(); +} |