summaryrefslogtreecommitdiff
path: root/lib/seq.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/seq.js')
-rw-r--r--lib/seq.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/seq.js b/lib/seq.js
new file mode 100644
index 0000000..966e751
--- /dev/null
+++ b/lib/seq.js
@@ -0,0 +1,28 @@
+'use strict';
+
+import noop from 'lodash/noop';
+import rest from 'lodash/rest';
+import reduce from './reduce';
+
+export default function seq( /* functions... */ ) {
+ var fns = arguments;
+ return rest(function(args) {
+ var that = this;
+
+ var cb = args[args.length - 1];
+ if (typeof cb == 'function') {
+ args.pop();
+ } else {
+ cb = noop;
+ }
+
+ reduce(fns, args, function(newargs, fn, cb) {
+ fn.apply(that, newargs.concat([rest(function(err, nextargs) {
+ cb(err, nextargs);
+ })]));
+ },
+ function(err, results) {
+ cb.apply(that, [err].concat(results));
+ });
+ });
+}