From 8bccdad52ddfaf3f7c6f44f759f50dc3672a5427 Mon Sep 17 00:00:00 2001 From: bekzod Date: Thu, 1 Jun 2017 20:01:24 +0500 Subject: [PATCH] move array type checking from `enumerator` to `.all` --- lib/es6-promise/enumerator.js | 28 ++++++++++------------------ lib/es6-promise/promise/all.js | 9 ++++++++- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/es6-promise/enumerator.js b/lib/es6-promise/enumerator.js index ca8b05e..5d91359 100644 --- a/lib/es6-promise/enumerator.js +++ b/lib/es6-promise/enumerator.js @@ -30,31 +30,23 @@ function Enumerator(Constructor, input) { makePromise(this.promise); } - if (isArray(input)) { - this._input = input; - this.length = input.length; - this._remaining = input.length; + this._input = input; + 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; - this._enumerate(); - if (this._remaining === 0) { - 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() { let { length, _input } = this; 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; }