'use strict'

var isPromise = require('./util/isPromise')
var limitConcurrency = require('./util/limitConcurrency')

function map (array, fn, options) {
  options = options || {concurrency: Infinity}

  return limitConcurrency(options.concurrency, array.map((value, i) => {
    return () => {
      if (isPromise(value)) {
        return value.then((value) => fn(value, i, array.length))
      }

      try {
        return fn(value, i, array.length)
      } catch (err) {
        return Promise.reject(err)
      }
    }
  }))
}

/*
  Iterates over the array and calls fn on each value
  (or promise that resolves to a value) in parallel.

*/

module.exports = (array, fn, options) => {
  if (typeof array === 'function') {
    fn = array
    return (array) => map(array, fn, options)
  }

  return map(array, fn, options)
}

@@ -268,21 +268,8 @@
 ) =%3E
- %7B%0A    return
  () 
@@ -496,22 +496,16 @@
       %7D%0A
-    %7D%0A
   %7D))%0A%7D%0A

update promise/map — streamline