diff --git a/lib/es6-promise/enumerator.js b/lib/es6-promise/enumerator.js index df4faca..726a4fc 100644 --- a/lib/es6-promise/enumerator.js +++ b/lib/es6-promise/enumerator.js @@ -30,13 +30,17 @@ function Enumerator(Constructor, input) { makePromise(this.promise); } - if (isArray(input)) { - this.length = input.length; - this._remaining = input.length; + this.length = input.length; + this._remaining = input.length; - this._result = new Array(this.length); + this._result = new Array(this.length); - if (this.length === 0) { + if (this.length === 0) { + fulfill(this.promise, this._result); + } else { + this.length = this.length || 0; + this._enumerate(); + if (this._remaining === 0) { fulfill(this.promise, this._result); } else { this.length = this.length || 0; @@ -45,15 +49,9 @@ function Enumerator(Constructor, input) { fulfill(this.promise, this._result); } } - } else { - reject(this.promise, validationError()); } } -function validationError() { - return new Error('Array Methods must be provided an Array'); -}; - Enumerator.prototype._enumerate = function(input) { for (let i = 0; this._state === PENDING && i < input.length; i++) { this._eachEntry(input[i], i); diff --git a/lib/es6-promise/promise/all.js b/lib/es6-promise/promise/all.js index 9ca3c06..315c42c 100644 --- a/lib/es6-promise/promise/all.js +++ b/lib/es6-promise/promise/all.js @@ -1,5 +1,8 @@ import Enumerator from '../enumerator'; - +import { + isArray, + isMaybeThenable +} from '../utils'; /** `Promise.all` accepts an array of promises, and returns a new promise which is fulfilled with an array of fulfillment values for the passed promises, or @@ -48,5 +51,9 @@ import Enumerator from '../enumerator'; @static */ export default function all(entries) { + if (!isArray(entries)){ + return new Constructor((_, reject) => reject(new TypeError('You must pass an array to all.'))); + } + return new Enumerator(this, entries).promise; }