@@ -132,8 +132,9 @@ Array.fromAsync and Promise.all, which have complementary control flows:
132
132
133
133
Also like ` for await ` , when given a sync-but-not-async iterable input, then
134
134
Array.fromAsync will catch ** only** the first rejection that its iteration
135
- reaches, and only if that rejection does ** not** occur in a microtask before the
136
- iteration reaches and awaits for it. For more information, see [ § Errors] [ ] .
135
+ reaches, and only if that rejection does ** not** occur in a microtask before
136
+ the iteration reaches and awaits for it. For more information, see
137
+ [ § Errors] [ ] .
137
138
138
139
``` js
139
140
// `arr` will be `[ 0, 2, 4, 6 ]`.
@@ -149,16 +150,16 @@ const arr = await Promise.all(Array.from(genPromises(4)));
149
150
```
150
151
151
152
### Non-iterable array-like inputs
152
- Array.fromAsync’s valid inputs are a superset of Array.from’s valid inputs. This
153
- includes non-iterable array-likes: objects that have a length property as well
154
- as indexed elements (similarly to Array.prototype.values). The return value is
155
- still a promise that will resolve to an array. If the array-like object’s
156
- elements are promises, then each accessed promise is awaited before its value is
157
- added to the new array.
153
+ Array.fromAsync’s valid inputs are a superset of Array.from’s valid inputs.
154
+ This includes non-iterable array-likes: objects that have a length property as
155
+ well as indexed elements (similarly to Array.prototype.values). The return
156
+ value is still a promise that will resolve to an array. If the array-like
157
+ object’s elements are promises, then each accessed promise is awaited before
158
+ its value is added to the new array.
158
159
159
160
One [ TC39 representative’s opinion] [ issue #7 comment ] : “[ Array-likes are] very
160
- much not obsolete, and it’s very nice that things aren’t forced to implement the
161
- iterator protocol to be transformable into an Array.”
161
+ much not obsolete, and it’s very nice that things aren’t forced to implement
162
+ the iterator protocol to be transformable into an Array.”
162
163
163
164
[ issue #7 comment ] : https://github.com/tc39/proposal-array-from-async/issues/7#issuecomment-920299880
164
165
@@ -193,8 +194,8 @@ by any other constructor. In that case, the final result will be the data
193
194
structure created by that constructor (with no arguments), and with each value
194
195
yielded by the input being assigned to the data structure’s numeric properties.
195
196
(Symbol.species is not involved at all.) If the this receiver is not a
196
- constructor, then fromAsync creates an array as usual. This matches the behavior
197
- of Array.from.
197
+ constructor, then fromAsync creates an array as usual. This matches the
198
+ behavior of Array.from.
198
199
199
200
``` js
200
201
async function * asyncGen (n ) {
@@ -287,9 +288,9 @@ Array.fromAsync(genError());
287
288
```
288
289
289
290
When Array.fromAsync’s input is synchronous only (i.e., the input is not an
290
- async iterable), and when one of the input’s values is a promise that eventually
291
- rejects or has rejected, then iteration stops and Array.fromAsync’s returned
292
- promise will reject with the first such error.
291
+ async iterable), and when one of the input’s values is a promise that
292
+ eventually rejects or has rejected, then iteration stops and Array.fromAsync’s
293
+ returned promise will reject with the first such error.
293
294
294
295
In this case, Array.fromAsync will catch and handle that first input rejection
295
296
** only if** that rejection does ** not** occur in a microtask before the
@@ -301,13 +302,14 @@ function * genRejection () {
301
302
yield Promise .reject (err);
302
303
}
303
304
304
- // This returns a promise that will reject with `err`. There is **no** unhandled
305
- // promise rejection, because the rejection occurs in the same microtask.
305
+ // This returns a promise that will reject with `err`. There is **no**
306
+ // unhandled promise rejection, because the rejection occurs in the same
307
+ // microtask.
306
308
Array .fromAsync (genZeroThenRejection ());
307
309
```
308
310
309
- Just like with ` for await ` , Array.fromAsync will ** not** catch any rejections by
310
- the input’s promises whenever those rejections occur ** before** the ticks in
311
+ Just like with ` for await ` , Array.fromAsync will ** not** catch any rejections
312
+ by the input’s promises whenever those rejections occur ** before** the ticks in
311
313
which Array.fromAsync’s iteration reaches those promises.
312
314
313
315
This is because – like ` for await ` – Array.fromAsync ** lazily** iterates over
@@ -317,10 +319,10 @@ developer needs to choose carefully between Array.fromAsync and Promise.all,
317
319
which have complementary control flows (see [ § Sync-iterable
318
320
inputs] ( #sync-iterable-inputs ) ).
319
321
320
- For example, when a synchronous input contains two promises, the latter of which
321
- will reject before the former promise resolves, then Array.fromAsync will not
322
- catch that rejection, because it lazily reaches the rejecting promise only after
323
- it already has rejected.
322
+ For example, when a synchronous input contains two promises, the latter of
323
+ which will reject before the former promise resolves, then Array.fromAsync will
324
+ not catch that rejection, because it lazily reaches the rejecting promise only
325
+ after it already has rejected.
324
326
325
327
``` js
326
328
const numOfMillisecondsPerSecond = 1000 ;
@@ -395,16 +397,16 @@ asyncGen().toArray()
395
397
396
398
toArray overlaps with both Array.from and Array.fromAsync. This is okay. They
397
399
can coexist. If we have to choose between having toArray and having fromAsync,
398
- then we should choose fromAsync. We already have Array.from. We should match the
399
- existing language precedent.
400
+ then we should choose fromAsync. We already have Array.from. We should match
401
+ the existing language precedent.
400
402
401
403
A [ co-champion of iterable-helpers agrees] [ tc39/proposal-iterator-helpers#156 ]
402
- that we should have both or that we should prefer Array.fromAsync: “I remembered
403
- why it’s better for a buildable structure to consume an iterable than for an
404
- iterable to consume a buildable protocol. Sometimes building something one
405
- element at a time is the same as building it [ more than one] element at a time,
406
- but sometimes it could be slow to build that way or produce a structure with
407
- equivalent semantics but different performance properties.”
404
+ that we should have both or that we should prefer Array.fromAsync: “I
405
+ remembered why it’s better for a buildable structure to consume an iterable
406
+ than for an iterable to consume a buildable protocol. Sometimes building
407
+ something one element at a time is the same as building it [ more than one]
408
+ element at a time, but sometimes it could be slow to build that way or produce
409
+ a structure with equivalent semantics but different performance properties.”
408
410
409
411
[ iterator-helpers ] : https://github.com/tc39/proposal-iterator-helpers
410
412
[ tc39/proposal-iterator-helpers#156 ] : https://github.com/tc39/proposal-iterator-helpers/issues/156.
@@ -424,8 +426,8 @@ See [issue #8][] and [proposal-setmap-offrom][].
424
426
[ proposal-setmap-offrom ] : https://github.com/tc39/proposal-setmap-offrom
425
427
426
428
### Async spread operator
427
- In the future, standardizing an async spread operator (like ` [ 0, await ...v ] ` )
428
- may be useful. This proposal leaves that idea to a ** separate** proposal.
429
+ In the future, standardizing an async spread operator (like `[ 0, await ...v
430
+ ] `) may be useful. This proposal leaves that idea to a ** separate** proposal.
429
431
430
432
### Records and tuples
431
433
The ** [ record/tuple] proposal** puts forward two new data types with APIs that
0 commit comments