import tryFn from './tryFn';
import PromiseBreak from './promiseBreak';

export default function retry({ times = 5, interval = 0 }, task, ...args) {
    if (typeof interval != 'function') {
        interval = (function(interval) {
            return function() { return interval; };
        })(interval);
    }

    return new Array(times).fill(null)
        .reduce(
            (promise, item, index) => {
                return promise.then((results) => {
                    return tryFn(task, results, ...args)
                        .then((result) => {
                            throw new PromiseBreak(result);
                        })
                        .catch((error) => {
                            if (error instanceof PromiseBreak) {
                                throw error;
                            }

                            results.push(error);

                            if (index < (times - 1)) {
                                return new Promise(
                                    (resolve) => setTimeout(() => resolve(results), interval(index + 1))
                                );
                            }

                            throw error;
                        });
                });
            },
            Promise.resolve([])
        )
            .catch((error) => {
                if (error instanceof PromiseBreak) {
                    return Promise.resolve(error.value);
                }
                throw error;
            });
};
@@ -100,53 +100,435 @@
 try(
-%7B times = 5, interval = 0 %7D, task, ...args) %7B
+opts, ...args) %7B%0A    let task;%0A%0A    if (typeof opts == 'function') %7B%0A        task = opts;%0A        opts = %7B%0A            times: 5,%0A            interval: 0%0A        %7D;%0A    %7D%0A%0A    if (Number.isInteger(opts)) %7B%0A        opts = %7B%0A            times: opts,%0A            interval: 0%0A        %7D;%0A    %7D%0A%0A    if (!Number.isInteger(opts.times)) %7B%0A        opts.times = 5;%0A    %7D%0A%0A    if (opts.interval == null) %7B%0A        opts.interval = 0;%0A    %7D%0A
 %0A   
@@ -539,16 +539,21 @@
 (typeof 
+opts.
 interval
@@ -570,32 +570,37 @@
 ion') %7B%0A        
+opts.
 interval = (func
@@ -679,16 +679,21 @@
      %7D)(
+opts.
 interval
@@ -698,24 +698,84 @@
 al);%0A    %7D%0A%0A
+    if (task == null) %7B%0A        task = args.shift();%0A    %7D%0A%0A
     return n
@@ -783,16 +783,21 @@
 w Array(
+opts.
 times).f
@@ -1378,16 +1378,21 @@
 ndex %3C (
+opts.
 times - 
@@ -1533,16 +1533,21 @@
 sults), 
+opts.
 interval
@@ -1556,16 +1556,25 @@
 ndex + 1
+, results
 ))%0A     
@@ -1987,8 +1987,9 @@
   %7D);%0A%7D;
+%0A

Update retry to better support different variations of allowed arguments