
var apply = require('lift-result/apply')
var ResultType = require('result-type')
var lift = require('lift-result')
var Result = require('result')
var each = require('./index')

/**
 * parallel each
 *
 * @param {Object|Array} obj
 * @param {Function} fn(value, key)
 * @param {Object} [context]
 * @return {Result}
 */

module.exports = lift(function(obj, fn, ctx){
	var result = new Result
	var done = false
	var pending = 0

	function fail(e){
		result.error(e)
	}

	function write(){
		if (--pending === 0 && done) result.write()
	}

	each(obj, function(value, key){
		try { var ret = apply.call(ctx, value, key, fn) }
		catch (e) { return fail(e) }
		if (ret instanceof ResultType) {
			pending++
			ret.read(write, fail)
		}
	})

	if (pending === 0) result.write()
	else done = true

	return result
})
@@ -389,26 +389,8 @@
 ult%0A
-%09var done = false%0A
 %09var
@@ -488,16 +488,8 @@
 == 0
- && done
 ) re
@@ -552,13 +552,11 @@
 %7B va
-r ret
+lue
  = a
@@ -625,19 +625,21 @@
 %7D%0A%09%09if (
-ret
+value
  instanc
@@ -672,19 +672,21 @@
 ng++%0A%09%09%09
-ret
+value
 .read(wr
@@ -743,26 +743,8 @@
 te()
-%0A%09else done = true
 %0A%0A%09r

remove unnecessary `done` variable