Skip to content

Commit 48cc8f5

Browse files
authored
Merge pull request #1122 from zloirock/array-from-async
2 parents 684ab08 + 1b0f623 commit 48cc8f5

File tree

10 files changed

+49
-29
lines changed

10 files changed

+49
-29
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Changelog
22
##### Unreleased
3+
- [`Array.fromAsync` proposal](https://github.com/tc39/proposal-array-from-async):
4+
- Moved to Stage 3, [September TC39 meeting](https://github.com/babel/proposals/issues/83#issuecomment-1246218703)
5+
- Avoid observable side effects of `%Array.prototype.values%` usage in array-like branch, [proposal-array-from-async/30](https://github.com/tc39/proposal-array-from-async/pull/30)
36
- Added `inverse` option to `core-js-compat`, [#1119](https://github.com/zloirock/core-js/issues/1119)
47
- Added `format` option to `core-js-builder`, [#1120](https://github.com/zloirock/core-js/issues/1120)
58

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ queueMicrotask(() => console.log('called as microtask'));
126126
- [`Symbol.prototype.description`](#symbolprototypedescription)
127127
- [Well-formed `JSON.stringify`](#well-formed-jsonstringify)
128128
- [Stage 3 proposals](#stage-3-proposals)
129+
- [`Array.fromAsync`](#arrayfromasync)
129130
- [`Array` grouping](#array-grouping)
130131
- [Change `Array` by copy](#change-array-by-copy)
131132
- [Stage 2 proposals](#stage-2-proposals)
132133
- [`Iterator` helpers](#iterator-helpers)
133134
- [New `Set` methods](#new-set-methods)
134135
- [`Map.prototype.emplace`](#mapprototypeemplace)
135-
- [`Array.fromAsync`](#arrayfromasync)
136136
- [`Array.isTemplateObject`](#arrayistemplateobject)
137137
- [`Symbol.{ asyncDispose, dispose }` for `using` statement](#symbol-asyncdispose-dispose--for-using-statement)
138138
- [`Symbol.metadataKey` for decorators metadata proposal](#symbolmetadatakey-for-decorators-metadata-proposal)
@@ -2103,6 +2103,22 @@ core-js/proposals/well-formed-stringify
21032103
```js
21042104
core-js(-pure)/stage/3
21052105
```
2106+
##### [`Array.fromAsync`](https://github.com/tc39/proposal-array-from-async)[⬆](#index)
2107+
Modules [`esnext.array.from-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.from-async.js).
2108+
```js
2109+
class Array {
2110+
static fromAsync(asyncItems: AsyncIterable | Iterable | ArrayLike, mapfn?: (value: any, index: number) => any, thisArg?: any): Array;
2111+
}
2112+
```
2113+
[*CommonJS entry points:*](#commonjs-api)
2114+
```js
2115+
core-js/proposals/array-from-async-stage-2
2116+
core-js(-pure)/full/array/from-async
2117+
```
2118+
[*Example*](https://goo.gl/Jt7SsD):
2119+
```js
2120+
await Array.fromAsync((async function * (){ yield * [1, 2, 3] })(), i => i * i); // => [1, 4, 9]
2121+
```
21062122
##### [`Array` grouping](https://github.com/tc39/proposal-array-grouping)[⬆](#index)
21072123
Modules [`esnext.array.group`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group.js), [`esnext.array.group-to-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group-to-map.js).
21082124
```js
@@ -2343,22 +2359,6 @@ map.emplace('b', { update: it => it ** 2, insert: () => 3}); // => 3
23432359

23442360
console.log(map); // => Map { 'a': 4, 'b': 3 }
23452361
```
2346-
##### [`Array.fromAsync`](https://github.com/tc39/proposal-array-from-async)[⬆](#index)
2347-
Modules [`esnext.array.from-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.from-async.js).
2348-
```js
2349-
class Array {
2350-
static fromAsync(asyncItems: AsyncIterable | Iterable | ArrayLike, mapfn?: (value: any, index: number) => any, thisArg?: any): Array;
2351-
}
2352-
```
2353-
[*CommonJS entry points:*](#commonjs-api)
2354-
```js
2355-
core-js/proposals/array-from-async-stage-2
2356-
core-js(-pure)/full/array/from-async
2357-
```
2358-
[*Example*](https://goo.gl/Jt7SsD):
2359-
```js
2360-
await Array.fromAsync((async function * (){ yield * [1, 2, 3] })(), i => i * i); // => [1, 4, 9]
2361-
```
23622362
##### [`Array.isTemplateObject`](https://github.com/tc39/proposal-array-is-template-object)[⬆](#index)
23632363
Module [`esnext.array.is-template-object`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.is-template-object.js)
23642364
```js
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require('../../modules/es.array.iterator');
2+
require('../../modules/es.object.to-string');
3+
require('../../modules/es.promise');
4+
require('../../modules/es.string.iterator');
5+
require('../../modules/esnext.array.from-async');
6+
var path = require('../../internals/path');
7+
8+
module.exports = path.Array.fromAsync;

packages/core-js/actual/array/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ require('../../modules/es.map');
33
require('../../modules/es.object.to-string');
44
require('../../modules/esnext.array.find-last');
55
require('../../modules/esnext.array.find-last-index');
6+
require('../../modules/esnext.array.from-async');
67
require('../../modules/esnext.array.group');
78
require('../../modules/esnext.array.group-by');
89
require('../../modules/esnext.array.group-by-to-map');
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
require('../../modules/es.array.iterator');
2-
require('../../modules/es.object.to-string');
3-
require('../../modules/es.promise');
4-
require('../../modules/es.string.iterator');
5-
require('../../modules/esnext.array.from-async');
6-
var path = require('../../internals/path');
1+
var parent = require('../../actual/array/from-async');
72

8-
module.exports = path.Array.fromAsync;
3+
module.exports = parent;

packages/core-js/full/array/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var parent = require('../../actual/array');
22
require('../../modules/es.promise');
3-
require('../../modules/esnext.array.from-async');
43
// TODO: Remove from `core-js@4`
54
require('../../modules/esnext.array.at');
65
// TODO: Remove from `core-js@4`

packages/core-js/internals/array-from-async.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
var bind = require('../internals/function-bind-context');
3+
var uncurryThis = require('../internals/function-uncurry-this');
34
var toObject = require('../internals/to-object');
45
var isConstructor = require('../internals/is-constructor');
56
var getAsyncIterator = require('../internals/get-async-iterator');
@@ -13,7 +14,20 @@ var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator');
1314
var toArray = require('../internals/async-iterator-iteration').toArray;
1415

1516
var ASYNC_ITERATOR = wellKnownSymbol('asyncIterator');
16-
var arrayIterator = getVirtual('Array').values;
17+
var arrayIterator = uncurryThis(getVirtual('Array').values);
18+
var arrayIteratorNext = uncurryThis(arrayIterator([]).next);
19+
20+
var safeArrayIterator = function () {
21+
return new SafeArrayIterator(this);
22+
};
23+
24+
var SafeArrayIterator = function (O) {
25+
this.iterator = arrayIterator(O);
26+
};
27+
28+
SafeArrayIterator.prototype.next = function () {
29+
return arrayIteratorNext(this.iterator);
30+
};
1731

1832
// `Array.fromAsync` method implementation
1933
// https://github.com/tc39/proposal-array-from-async
@@ -26,7 +40,7 @@ module.exports = function fromAsync(asyncItems /* , mapfn = undefined, thisArg =
2640
var O = toObject(asyncItems);
2741
if (mapfn !== undefined) mapfn = bind(mapfn, thisArg);
2842
var usingAsyncIterator = getMethod(O, ASYNC_ITERATOR);
29-
var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || arrayIterator;
43+
var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || safeArrayIterator;
3044
var A = isConstructor(C) ? new C() : [];
3145
var iterator = usingAsyncIterator
3246
? getAsyncIterator(O, usingAsyncIterator)

packages/core-js/stage/2.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require('../proposals/array-from-async-stage-2');
21
require('../proposals/array-is-template-object');
32
require('../proposals/decorator-metadata');
43
require('../proposals/iterator-helpers');

packages/core-js/stage/3.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require('../proposals/array-from-async-stage-2');
12
require('../proposals/array-grouping-stage-3-2');
23
require('../proposals/change-array-by-copy');
34
// TODO: Obsolete versions, remove from `core-js@4`

tests/commonjs.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
596596
}
597597

598598
for (const NS of ['actual', 'full', 'features']) {
599+
ok(typeof load(NS, 'array/from-async') == 'function');
599600
ok(typeof load(NS, 'array/group') == 'function');
600601
ok(typeof load(NS, 'array/group-to-map') == 'function');
601602
ok(typeof load(NS, 'array/group-by') == 'function');
@@ -667,7 +668,6 @@ for (PATH of ['core-js-pure', 'core-js']) {
667668
const Set = load(NS, 'set');
668669
const WeakMap = load(NS, 'weak-map');
669670
const WeakSet = load(NS, 'weak-set');
670-
ok(typeof load(NS, 'array/from-async') == 'function');
671671
ok(typeof load(NS, 'array/filter-out') == 'function');
672672
ok(typeof load(NS, 'array/filter-reject') == 'function');
673673
ok(typeof load(NS, 'array/is-template-object') == 'function');

0 commit comments

Comments
 (0)