Skip to content

Commit

Permalink
move early { Array, %TypedArray% }.fromAsync errors to the promise,…
Browse files Browse the repository at this point in the history
… per the latest changes of the spec draft
  • Loading branch information
zloirock committed Oct 3, 2021
1 parent 57a189e commit 2be1c36
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 14 additions & 9 deletions packages/core-js/internals/array-from-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
});
};
11 changes: 9 additions & 2 deletions packages/core-js/modules/esnext.typed-array.from-async.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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);
20 changes: 14 additions & 6 deletions tests/pure/esnext.array.from-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
20 changes: 14 additions & 6 deletions tests/tests/esnext.array.from-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
26 changes: 18 additions & 8 deletions tests/tests/esnext.typed-array.from-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}

0 comments on commit 2be1c36

Please sign in to comment.