From 2be1c369de89652ee08e5f4b35ae19b00d600c9a Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Mon, 4 Oct 2021 06:42:43 +0700 Subject: [PATCH] move early `{ Array, %TypedArray% }.fromAsync` errors to the promise, per the latest changes of the spec draft --- CHANGELOG.md | 1 + .../core-js/internals/array-from-async.js | 23 +++++++++------- .../modules/esnext.typed-array.from-async.js | 11 ++++++-- tests/pure/esnext.array.from-async.js | 20 +++++++++----- tests/tests/esnext.array.from-async.js | 20 +++++++++----- tests/tests/esnext.typed-array.from-async.js | 26 +++++++++++++------ 6 files changed, 70 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f44567e6f68b..90c481bfa6fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Changelog ##### Unreleased +- Early `{ Array, %TypedArray% }.fromAsync` errors moved to the promise, per the latest changes of the spec draft - Some minor fixes and improvements ##### 3.18.1 - 2021.09.27 diff --git a/packages/core-js/internals/array-from-async.js b/packages/core-js/internals/array-from-async.js index 786f111d6200..8cffce455d38 100644 --- a/packages/core-js/internals/array-from-async.js +++ b/packages/core-js/internals/array-from-async.js @@ -7,6 +7,7 @@ var getIterator = require('../internals/get-iterator'); var getIteratorMethod = require('../internals/get-iterator-method'); var getMethod = require('../internals/get-method'); var getVirtual = require('../internals/entry-virtual'); +var getBuiltIn = require('../internals/get-built-in'); var wellKnownSymbol = require('../internals/well-known-symbol'); var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator'); var toArray = require('../internals/async-iterator-iteration').toArray; @@ -17,15 +18,19 @@ var arrayIterator = getVirtual('Array').values; // `Array.fromAsync` method implementation // https://github.com/tc39/proposal-array-from-async module.exports = function fromAsync(asyncItems /* , mapfn = undefined, thisArg = undefined */) { - var O = toObject(asyncItems); + var C = this; var argumentsLength = arguments.length; var mapfn = argumentsLength > 1 ? arguments[1] : undefined; - if (mapfn !== undefined) mapfn = bind(mapfn, argumentsLength > 2 ? arguments[2] : undefined, 2); - var usingAsyncIterator = getMethod(O, ASYNC_ITERATOR); - var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || arrayIterator; - var A = isConstructor(this) ? new this() : []; - var iterator = usingAsyncIterator - ? getAsyncIterator(O, usingAsyncIterator) - : new AsyncFromSyncIterator(getIterator(O, usingSyncIterator)); - return toArray(iterator, mapfn, A); + var thisArg = argumentsLength > 2 ? arguments[2] : undefined; + return new (getBuiltIn('Promise'))(function (resolve) { + var O = toObject(asyncItems); + if (mapfn !== undefined) mapfn = bind(mapfn, thisArg, 2); + var usingAsyncIterator = getMethod(O, ASYNC_ITERATOR); + var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || arrayIterator; + var A = isConstructor(C) ? new C() : []; + var iterator = usingAsyncIterator + ? getAsyncIterator(O, usingAsyncIterator) + : new AsyncFromSyncIterator(getIterator(O, usingSyncIterator)); + resolve(toArray(iterator, mapfn, A)); + }); }; diff --git a/packages/core-js/modules/esnext.typed-array.from-async.js b/packages/core-js/modules/esnext.typed-array.from-async.js index a1b3446a03fa..063b49c96064 100644 --- a/packages/core-js/modules/esnext.typed-array.from-async.js +++ b/packages/core-js/modules/esnext.typed-array.from-async.js @@ -1,4 +1,5 @@ 'use strict'; +var getBuiltIn = require('../internals/get-built-in'); var aConstructor = require('../internals/a-constructor'); var arrayFromAsync = require('../internals/array-from-async'); var TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS = require('../internals/typed-array-constructors-require-wrappers'); @@ -12,8 +13,14 @@ var exportTypedArrayStaticMethod = ArrayBufferViewCore.exportTypedArrayStaticMet // https://github.com/tc39/proposal-array-from-async // eslint-disable-next-line -- required for .length exportTypedArrayStaticMethod('fromAsync', function fromAsync(asyncItems /* , mapfn = undefined, thisArg = undefined */) { - var C = aConstructor(this); - return arrayFromAsync.apply(Array, arguments).then(function (list) { + var C = this; + var argumentsLength = arguments.length; + var mapfn = argumentsLength > 1 ? arguments[1] : undefined; + var thisArg = argumentsLength > 2 ? arguments[2] : undefined; + return new (getBuiltIn('Promise'))(function (resolve) { + aConstructor(C); + resolve(arrayFromAsync(asyncItems, mapfn, thisArg)); + }).then(function (list) { return arrayFromConstructorAndList(aTypedArrayConstructor(C), list); }); }, TYPED_ARRAYS_CONSTRUCTORS_REQUIRES_WRAPPERS); diff --git a/tests/pure/esnext.array.from-async.js b/tests/pure/esnext.array.from-async.js index 41bb264c74ab..d5d74f6f4f8f 100644 --- a/tests/pure/esnext.array.from-async.js +++ b/tests/pure/esnext.array.from-async.js @@ -56,10 +56,18 @@ QUnit.test('Array.fromAsync', assert => { return fromAsync(createIterable([1]), () => { throw 42; }); }).catch(error => { assert.same(error, 42, 'rejection on a callback error'); - }).then(() => async()); - - assert.throws(() => fromAsync(undefined, () => { /* empty */ }), TypeError); - assert.throws(() => fromAsync(null, () => { /* empty */ }), TypeError); - assert.throws(() => fromAsync([1], null), TypeError); - assert.throws(() => fromAsync([1], {}), TypeError); + return fromAsync(undefined, () => { /* empty */ }); + }).catch(error => { + assert.ok(error instanceof TypeError); + return fromAsync(null, () => { /* empty */ }); + }).catch(error => { + assert.ok(error instanceof TypeError); + return fromAsync([1], null); + }).catch(error => { + assert.ok(error instanceof TypeError); + return fromAsync([1], {}); + }).catch(error => { + assert.ok(error instanceof TypeError); + async(); + }); }); diff --git a/tests/tests/esnext.array.from-async.js b/tests/tests/esnext.array.from-async.js index b5364abd6cc6..d36f12d24df3 100644 --- a/tests/tests/esnext.array.from-async.js +++ b/tests/tests/esnext.array.from-async.js @@ -57,10 +57,18 @@ QUnit.test('Array.fromAsync', assert => { return fromAsync(createIterable([1]), () => { throw 42; }); }).catch(error => { assert.same(error, 42, 'rejection on a callback error'); - }).then(() => async()); - - assert.throws(() => fromAsync(undefined, () => { /* empty */ }), TypeError); - assert.throws(() => fromAsync(null, () => { /* empty */ }), TypeError); - assert.throws(() => fromAsync([1], null), TypeError); - assert.throws(() => fromAsync([1], {}), TypeError); + return fromAsync(undefined, () => { /* empty */ }); + }).catch(error => { + assert.ok(error instanceof TypeError); + return fromAsync(null, () => { /* empty */ }); + }).catch(error => { + assert.ok(error instanceof TypeError); + return fromAsync([1], null); + }).catch(error => { + assert.ok(error instanceof TypeError); + return fromAsync([1], {}); + }).catch(error => { + assert.ok(error instanceof TypeError); + async(); + }); }); diff --git a/tests/tests/esnext.typed-array.from-async.js b/tests/tests/esnext.typed-array.from-async.js index d307e8753161..2883750fcd79 100644 --- a/tests/tests/esnext.typed-array.from-async.js +++ b/tests/tests/esnext.typed-array.from-async.js @@ -55,13 +55,23 @@ if (DESCRIPTORS) { return TypedArray.fromAsync(createIterable([1]), () => { throw 42; }); }).catch(error => { assert.same(error, 42, 'rejection on a callback error'); - }).then(() => async()); - - function C() { /* empty */ } - assert.throws(() => TypedArray.fromAsync.call(C, [1], {}), TypeError); - assert.throws(() => TypedArray.fromAsync(undefined, () => { /* empty */ }), TypeError); - assert.throws(() => TypedArray.fromAsync(null, () => { /* empty */ }), TypeError); - assert.throws(() => TypedArray.fromAsync([1], null), TypeError); - assert.throws(() => TypedArray.fromAsync([1], {}), TypeError); + function C() { /* empty */ } + return TypedArray.fromAsync.call(C, [1], {}); + }).catch(error => { + assert.ok(error instanceof TypeError); + return TypedArray.fromAsync(undefined, () => { /* empty */ }); + }).catch(error => { + assert.ok(error instanceof TypeError); + return TypedArray.fromAsync(null, () => { /* empty */ }); + }).catch(error => { + assert.ok(error instanceof TypeError); + return TypedArray.fromAsync([1], null); + }).catch(error => { + assert.ok(error instanceof TypeError); + return TypedArray.fromAsync([1], {}); + }).catch(error => { + assert.ok(error instanceof TypeError); + async(); + }); }); }