import chunkify from '../index'
import ChunkifyOptions from '../options'
import _ from 'underscore'


const USAGE = 'Usage: chunkify.interval(Function fn, Number final, [Object options])';

let ok_usage = (fn, final, options) => {
  if (!_.isFunction(fn)) {
    throw new Error(`${USAGE} - bad fn; not a function`);
  } else  if (!_.isNumber(final)) {
    throw new Error(`${USAGE} - bad final; not a number`);
  }
  let {start} = options;
  if (start != null) {
    if (!_.isNumber(start)) {
      throw new Error(`${USAGE} - bad start; not a number`);
    } else if (start > final) {
      throw new Error(`${USAGE} - bad start; it's greater than final`);
    }
  }
};

let interval = (fn, final, options = {}) => {
  ok_usage(fn, final, options);
  let start = options.start || 0;
  let okoptions = ChunkifyOptions.of(options);
  let generator = chunkify.generator(start, final, okoptions);
  var process_chunk_sync = (resolve, reject) => {
    let next = generator.next();
    while (!(next.value instanceof Promise) && !next.done) {
      try {
        fn.call(okoptions.scope, next.value)
      } catch (error) {
        return reject({error, index: next.value});
      }
      next = generator.next();
    }
    if (next.done) {
      return resolve();
    }
    return next.value.then(() => {
      return process_chunk_sync(resolve, reject)
    });
  };
  return new Promise(process_chunk_sync);
};

export default interval
@@ -77,26 +77,53 @@
 ort 
-_ from 'underscore
+%7B%0A  isFunction,%0A  isNumber%0A%7D from '../utility
 '%0A%0A%0A
@@ -214,20 +214,22 @@
 ';%0A%0Alet 
-ok_u
+checkU
 sage = (
@@ -260,18 +260,16 @@
 %0A  if (!
-_.
 isFuncti
@@ -347,26 +347,24 @@
  else  if (!
-_.
 isNumber(fin
@@ -494,10 +494,8 @@
 f (!
-_.
 isNu
@@ -740,12 +740,14 @@
 %7B%0A  
-ok_u
+checkU
 sage
@@ -808,19 +808,19 @@
 ;%0A  let 
-oko
+chO
 ptions =
@@ -900,19 +900,19 @@
  final, 
-oko
+chO
 ptions);
@@ -925,24 +925,22 @@
  process
-_c
+C
 hunk
-_s
+S
 ync = (r
@@ -1086,11 +1086,11 @@
 all(
-oko
+chO
 ptio
@@ -1337,24 +1337,22 @@
  process
-_c
+C
 hunk
-_s
+S
 ync(reso
@@ -1409,16 +1409,14 @@
 cess
-_c
+C
 hunk
-_s
+S
 ync)

replace _ deps; style