import tryFn from './tryFn';
import WaterfallError from './waterfallError';

export default function waterfall(tasks, ...args) {
    let
        results = [];

    return tasks.reduce(
        (promise, task) => promise.then((promiseResults) => {
            return tryFn(task, ...promiseResults)
                .then((taskResults) => {
                    return results = Array.isArray(taskResults)
                        ? taskResults
                        : [taskResults];
                });
        }),
        Promise.resolve(args)
    )
        .catch((error) => {
            let waterfallError = new WaterfallError();
            waterfallError.message = error.message;
            waterfallError.results = results;
            throw waterfallError;
        });
};
@@ -122,16 +122,158 @@
 args) %7B%0A
+    if (!Array.isArray(tasks)) %7B%0A        return Promise.reject(new Error('First argument to waterfall must be an array of functions'))%0A    %7D%0A%0A
     let%0A
@@ -342,16 +342,23 @@
 se, task
+, index
 ) =%3E pro
@@ -472,32 +472,162 @@
 skResults) =%3E %7B%0A
+                    if (index == tasks.length - 1) %7B%0A                        return taskResults;%0A                    %7D else %7B%0A    
                 
@@ -698,16 +698,20 @@
         
+  
+  
 ? taskRe
@@ -716,16 +716,20 @@
 Results%0A
+    
         
@@ -761,16 +761,38 @@
 sults%5D;%0A
+                    %7D%0A
         
@@ -1080,8 +1080,9 @@
   %7D);%0A%7D;
+%0A

Update waterfall to return the last task results unchanged.