-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspec.html
632 lines (590 loc) · 30.5 KB
/
spec.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<!DOCTYPE html>
<meta charset="utf-8">
<pre class="metadata">
title: Async Iterator Helpers
status: proposal
stage: 2
location: https://tc39.es/proposal-async-iterator-helpers
copyright: false
contributors: Gus Caplan, Michael Ficarra, Kevin Gibbons
</pre>
<style>
ins.block {
font-weight: bold;
font-style: italic;
}
hr {
height: 0.25em;
background: #ccc;
border: 0;
margin: 2em 0;
}
.unicode-property-table {
table-layout: fixed;
width: 100%;
font-size: 80%;
}
.unicode-property-table ul {
padding-left: 0;
list-style: none;
}
#metadata-block {
margin: 4em 0;
padding: 10px;
border: 1px solid #ee8421;
}
#metadata-block h1 {
font-size: 1.5em;
margin-top: 0;
}
#metadata-block > ul {
list-style-type: none;
margin: 0; padding: 0;
}
</style>
<div id="metadata-block">
<h1>Contributing to this Proposal</h1>
<p>This proposal is developed on GitHub with the help of the ECMAScript community. There are a number of ways to contribute to the development of this specification:</p>
<ul>
<li>GitHub Repository: <a href="https://github.com/tc39/proposal-async-iterator-helpers">https://github.com/tc39/proposal-async-iterator-helpers</a></li>
<li>Issues: <a href="https://github.com/tc39/proposal-async-iterator-helpers/issues">All Issues</a>, <a href="https://github.com/tc39/proposal-async-iterator-helpers/issues/new">File a New Issue</a></li>
<li>Pull Requests: <a href="https://github.com/tc39/proposal-async-iterator-helpers/pulls">All Pull Requests</a>, <a href="https://github.com/tc39/proposal-async-iterator-helpers/pulls/new">Create a New Pull Request</a></li>
<li>
<p>Community:</p>
<ul>
<li>Discourse: <a href="https://es.discourse.group">https://es.discourse.group</a></li>
<li>Chat: <a href="https://github.com/tc39/how-we-work/blob/HEAD/matrix-guide.md">Matrix</a></li>
</ul>
</li>
</ul>
</div>
<emu-clause id="sec-well-known-intrinsic-objects">
<h1>Well-Known Intrinsic Objects</h1>
<emu-table id="table-7" caption="Well-Known Intrinsic Objects">
<table>
<tr>
<th>
Intrinsic Name
</th>
<th>
Global Name
</th>
<th>
ECMAScript Language Association
</th>
</tr>
<tr>
<td>
<ins>%AsyncIterator%</ins>
</td>
<td>
<ins>`AsyncIterator`</ins>
</td>
<td>
<ins>The `AsyncIterator` constructor (<emu-xref href="#sec-asynciterator-constructor"></emu-xref>)</ins>
</td>
</tr>
<tr>
<td>
%AsyncIteratorPrototype%
</td>
<td>
<ins>`AsyncIterator.prototype`</ins>
</td>
<td>
An object that all standard built-in async iterator objects indirectly inherit from
<p><ins>The initial value of the *"prototype"* data property of %AsyncIterator%; i.e., %AsyncIterator.prototype%</ins></p>
</td>
</tr>
</table>
</emu-table>
</emu-clause>
<emu-clause id="sec-control-abstraction-objects">
<h1>Control Abstraction Objects</h1>
<emu-clause id="sec-iteration">
<h1>Iteration</h1>
<emu-clause id="sec-iterator-abstract-operations">
<h1>Iterator Abstract Operations</h1>
<emu-clause id="sec-ifabruptcloseasynciterator" aoid="IfAbruptCloseAsyncIterator">
<h1>IfAbruptCloseAsyncIterator ( _value_, _iteratorRecord_ )</h1>
<p>IfAbruptCloseAsyncIterator is a shorthand for a sequence of algorithm steps that use an Iterator Record. An algorithm step of the form:</p>
<emu-alg>
1. IfAbruptCloseAsyncIterator(_value_, _iteratorRecord_).
</emu-alg>
<p>means the same thing as:</p>
<emu-alg>
1. If _value_ is an abrupt completion, then
1. Perform ? AsyncIteratorClose(_iteratorRecord_, _value_).
1. Return _value_.
1. Else if _value_ is a Completion Record, set _value_ to _value_.[[Value]].
</emu-alg>
</emu-clause>
<emu-clause id="sec-awaitnonprimitive" type="abstract operation">
<h1>
AwaitNonPrimitive (
_value_: an ECMAScript language value,
): either a normal completion containing an ECMAScript language value or a throw completion
</h1>
<dl class="header">
</dl>
<emu-alg>
1. If _value_ is an Object, return ? Await(_value_).
1. Else, return _value_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-asynciterator-objects">
<h1>AsyncIterator Objects</h1>
<emu-clause id="sec-asynciterator-constructor">
<h1>The AsyncIterator Constructor</h1>
<p>The <dfn>AsyncIterator</dfn> constructor:</p>
<ul>
<li>is <dfn>%AsyncIterator%</dfn>.</li>
<li>is the initial value of the *"AsyncIterator"* property of the global object.</li>
<li>is designed to be subclassable. It may be used as the value of an *extends* clause of a class definition.</li>
</ul>
<emu-clause id="sec-asynciterator">
<h1>AsyncIterator ( )</h1>
<p>When the `AsyncIterator` function is called, the following steps are taken:</p>
<emu-alg>
1. If NewTarget is *undefined* or the active function object, throw a *TypeError* exception.
1. Return ? OrdinaryCreateFromConstructor(NewTarget, *"%AsyncIterator.prototype%"*).
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-the-asynciterator-constructor">
<h1>Properties of the AsyncIterator Constructor</h1>
<emu-clause id="sec-asynciterator.prototype">
<h1>AsyncIterator.prototype</h1>
<p>The initial value of AsyncIterator.prototype is %AsyncIterator.prototype%.</p>
<p>This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *false* }.</p>
</emu-clause>
<emu-clause id="sec-asynciterator.from">
<h1>AsyncIterator.from ( _O_ )</h1>
<emu-alg>
1. If _O_ is a String, set _O_ to ! ToObject(_O_).
1. Let _iteratorRecord_ be ? GetIteratorFlattenable(_O_, ~async~).
1. Let _hasInstance_ be ? OrdinaryHasInstance(%AsyncIterator%, _iteratorRecord_.[[Iterator]]).
1. If _hasInstance_ is *true*, then
1. Return _iteratorRecord_.[[Iterator]].
1. Let _wrapper_ be OrdinaryObjectCreate(%WrapForValidAsyncIteratorPrototype%, « [[AsyncIterated]] »).
1. Set _wrapper_.[[AsyncIterated]] to _iteratorRecord_.
1. Return _wrapper_.
</emu-alg>
<emu-clause id="sec-wrapforvalidasynciteratorprototype-object">
<h1>The %WrapForValidAsyncIteratorPrototype% Object</h1>
<p>The <dfn>%WrapForValidAsyncIteratorPrototype%</dfn> object:</p>
<ul>
<li>is an ordinary object.</li>
<li>has a [[Prototype]] internal slot whose value is %AsyncIterator.prototype%.</li>
<li>has the following properties:</li>
</ul>
<emu-clause id="sec-wrapforvalidasynciteratorprototype.next">
<h1>%WrapForValidAsyncIteratorPrototype%.next ( )</h1>
<emu-alg>
1. Let _O_ be *this* value.
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Let _check_ be Completion(RequireInternalSlot(_O_, [[AsyncIterated]])).
1. IfAbruptRejectPromise(_check_, _promiseCapability_).
1. Let _result_ be Completion(IteratorNext(_O_.[[AsyncIterated]])).
1. IfAbruptRejectPromise(_result_, _promiseCapability_).
1. Return ? PromiseResolve(%Promise%, _result_).
</emu-alg>
</emu-clause>
<emu-clause id="sec-wrapforvalidasynciteratorprototype.return">
<h1>%WrapForValidAsyncIteratorPrototype%.return ( )</h1>
<emu-alg>
1. Let _O_ be *this* value.
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Let _check_ be Completion(RequireInternalSlot(_O_, [[AsyncIterated]])).
1. IfAbruptRejectPromise(_check_, _promiseCapability_).
1. Let _iterator_ be _O_.[[AsyncIterated]].[[Iterator]].
1. Assert: _iterator_ is an Object.
1. Let _returnMethod_ be Completion(GetMethod(_iterator_, *"return"*)).
1. IfAbruptRejectPromise(_returnMethod_, _promiseCapability_).
1. If _returnMethod_ is *undefined*, then
1. Return ! PromiseResolve(%Promise%, CreateIterResultObject(*undefined*, *true*)).
1. Let _result_ be Completion(Call(_returnMethod_, _iterator_)).
1. IfAbruptRejectPromise(_result_, _promiseCapability_).
1. Return ? PromiseResolve(%Promise%, _result_).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-async-iterator-helper-objects">
<h1>Async Iterator Helper Objects</h1>
<p>An Async Iterator Helper object is an ordinary object that represents a lazy transformation of some specific source async iterator object. There is not a named constructor for Async Iterator Helper objects. Instead, Async Iterator Helper objects are created by calling certain methods of AsyncIterator instance objects.</p>
<emu-clause id="sec-%asynciteratorhelperprototype%-object">
<h1>The %AsyncIteratorHelperPrototype% Object</h1>
<p>The <dfn>%AsyncIteratorHelperPrototype%</dfn> object:</p>
<ul>
<li>has properties that are inherited by all Async Iterator Helper Objects.</li>
<li>is an ordinary object.</li>
<li>has a [[Prototype]] internal slot whose value is %AsyncIterator.prototype%.</li>
<li>has the following properties:</li>
</ul>
<emu-clause id="sec-%asynciteratorhelperprototype%.next">
<h1>%AsyncIteratorHelperPrototype%.next ( )</h1>
<emu-alg>
1. Return AsyncGeneratorNext(*this* value, *"Async Iterator Helper"*, *undefined*).
</emu-alg>
</emu-clause>
<emu-clause id="sec-%asynciteratorhelperprototype%.return">
<h1>%AsyncIteratorHelperPrototype%.return ( )</h1>
<emu-alg>
1. Return AsyncGeneratorReturn(*this* value, *"Async Iterator Helper"*, *undefined*).
</emu-alg>
</emu-clause>
<emu-clause id="sec-%asynciteratorhelperprototype%-@@tostringtag">
<h1>%AsyncIteratorHelperPrototype% [ @@toStringTag ]</h1>
<p>The initial value of the @@toStringTag property is the String value *"Async Iterator Helper"*.</p>
<p>This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *true* }.</p>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-iteratorprototype">
<h1>Iterator.prototype</h1>
<emu-clause id="sec-iteratorprototype.toasync">
<h1>Iterator.prototype.toAsync ( )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _syncIteratorRecord_ be ? GetIteratorDirect(*this* value).
1. Let _asyncIteratorRecord_ be CreateAsyncFromSyncIterator(_syncIteratorRecord_).
1. Let _wrapper_ be OrdinaryObjectCreate(%WrapForValidAsyncIteratorPrototype%, « [[AsyncIterated]] »).
1. Set _wrapper_.[[AsyncIterated]] to _asyncIteratorRecord_.
1. Return _wrapper_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype">
<h1>AsyncIterator.prototype</h1>
<p>The <dfn>AsyncIterator prototype object</dfn>:</p>
<ul>
<li>is <dfn>%AsyncIterator.prototype%</dfn>.</li>
<li>has a [[Prototype]] internal slot whose value is %Object.prototype%.</li>
<li>is an ordinary object.</li>
</ul>
<emu-clause id="sec-asynciteratorprototype.constructor">
<h1>AsyncIterator.prototype.constructor</h1>
<p>The initial value of AsyncIterator.prototype.constructor is %AsyncIterator%.</p>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.map">
<h1>AsyncIterator.prototype.map ( _mapper_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _mapper_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Set _mapped_ to Completion(AwaitNonPrimitive(_mapped_)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Let _completion_ be Completion(Yield(_mapped_)).
1. IfAbruptCloseAsyncIterator(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateAsyncIteratorFromClosure(_closure_, *"Async Iterator Helper"*, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.filter">
<h1>AsyncIterator.prototype.filter ( _predicate_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _predicate_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _selected_ be Completion(Call(_predicate_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseAsyncIterator(_selected_, _iterated_).
1. Set _selected_ to Completion(AwaitNonPrimitive(_selected_)).
1. IfAbruptCloseAsyncIterator(_selected_, _iterated_).
1. If ToBoolean(_selected_) is *true*, then
1. Let _completion_ be Completion(Yield(_value_)).
1. IfAbruptCloseAsyncIterator(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateAsyncIteratorFromClosure(_closure_, *"Async Iterator Helper"*, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.take">
<h1>AsyncIterator.prototype.take ( _limit_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. Let _numLimit_ be ? ToNumber(_limit_).
1. If _numLimit_ is *NaN*, throw a *RangeError* exception.
1. Let _integerLimit_ be ! ToIntegerOrInfinity(_numLimit_).
1. If _integerLimit_ < 0, throw a *RangeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _integerLimit_ and performs the following steps when called:
1. Let _remaining_ be _integerLimit_.
1. Repeat,
1. If _remaining_ is 0, then
1. Return ? AsyncIteratorClose(_iterated_, NormalCompletion(*undefined*)).
1. If _remaining_ is not +∞, then
1. Set _remaining_ to _remaining_ - 1.
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _completion_ be Completion(Yield(? IteratorValue(_next_))).
1. IfAbruptCloseAsyncIterator(_completion_, _iterated_).
1. Return CreateAsyncIteratorFromClosure(_closure_, *"Async Iterator Helper"*, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.drop">
<h1>AsyncIterator.prototype.drop ( _limit_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. Let _numLimit_ be ? ToNumber(_limit_).
1. If _numLimit_ is *NaN*, throw a *RangeError* exception.
1. Let _integerLimit_ be ! ToIntegerOrInfinity(_numLimit_).
1. If _integerLimit_ < 0, throw a *RangeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _integerLimit_ and performs the following steps when called:
1. Let _remaining_ be _integerLimit_.
1. Repeat, while _remaining_ > 0,
1. If _remaining_ is not +∞, then
1. Set _remaining_ to _remaining_ - 1.
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _completion_ be Completion(Yield(? IteratorValue(_next_))).
1. IfAbruptCloseAsyncIterator(_completion_, _iterated_).
1. Return CreateAsyncIteratorFromClosure(_closure_, *"Async Iterator Helper"*, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.flatmap">
<h1>AsyncIterator.prototype.flatMap ( _mapper_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _mapper_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Set _mapped_ to Completion(AwaitNonPrimitive(_mapped_)).
1. IfAbruptCloseAsyncIterator(_mapped_, _iterated_).
1. Let _innerIterator_ be Completion(GetIteratorFlattenable(_mapped_, ~async~)).
1. IfAbruptCloseAsyncIterator(_innerIterator_, _iterated_).
1. Let _innerAlive_ be *true*.
1. Repeat, while _innerAlive_ is *true*,
1. Let _innerNextPromise_ be Completion(IteratorNext(_innerIterator_)).
1. IfAbruptCloseAsyncIterator(_innerNextPromise_, _iterated_).
1. Let _innerNext_ be Completion(Await(_innerNextPromise_)).
1. IfAbruptCloseAsyncIterator(_innerNext_, _iterated_).
1. Let _innerComplete_ be Completion(IteratorComplete(_innerNext_)).
1. IfAbruptCloseAsyncIterator(_innerComplete_, _iterated_).
1. If _innerComplete_ is *true*, then
1. Set _innerAlive_ to *false*.
1. Else,
1. Let _innerValue_ be Completion(IteratorValue(_innerNext_)).
1. IfAbruptCloseAsyncIterator(_innerValue_, _iterated_).
1. [id="step-async-iterator-flatmap-yield"] Let _completion_ be Completion(Yield(_innerValue_)).
1. If _completion_ is a return completion, then
1. Let _backupCompletion_ be Completion(IteratorClose(_innerIterator_, _completion_)).
1. IfAbruptCloseIterator(_backupCompletion_, _iterated_).
1. Return ? IteratorClose(_completion_, _iterated_).
1. Else if _completion_ is a throw completion, then
1. Assert: Awaiting _innerValue_ during the Yield on step <emu-xref href="#step-async-iterator-flatmap-yield"></emu-xref> threw.
1. Return ? IteratorClose(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Return CreateAsyncIteratorFromClosure(_closure_, *"Async Iterator Helper"*, %AsyncIteratorHelperPrototype%).
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.reduce">
<h1>AsyncIterator.prototype.reduce ( _reducer_ [ , _initialValue_ ] )</h1>
<p>This async method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_reducer_) is *false*, throw a *TypeError* exception.
1. If _initialValue_ is not present, then
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, throw a *TypeError* exception.
1. Let _accumulator_ be ? IteratorValue(_next_).
1. Let _counter_ be 1.
1. Else,
1. Let _accumulator_ be _initialValue_.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return _accumulator_.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_reducer_, *undefined*, « _accumulator_, _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _result_ to Completion(AwaitNonPrimitive(_result_)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _accumulator_ to _result_.
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.toarray">
<h1>AsyncIterator.prototype.toArray ( )</h1>
<p>This async method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. Let _items_ be a new empty List.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return CreateArrayFromList(_items_).
1. Let _value_ be ? IteratorValue(_next_).
1. Append _value_ to _items_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.foreach">
<h1>AsyncIterator.prototype.forEach ( _fn_ )</h1>
<p>This async method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _r_ be Completion(Call(_fn_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseAsyncIterator(_r_, _iterated_).
1. Set _r_ to Completion(AwaitNonPrimitive(r)).
1. IfAbruptCloseAsyncIterator(_r_, _iterated_).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.some">
<h1>AsyncIterator.prototype.some ( _predicate_ )</h1>
<p>This async method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *false*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_predicate_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _result_ to Completion(AwaitNonPrimitive(_result_)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *true*, return ? AsyncIteratorClose(_iterated_, NormalCompletion(*true*)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.every">
<h1>AsyncIterator.prototype.every ( _predicate_ )</h1>
<p>This async method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *true*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_predicate_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _result_ to Completion(AwaitNonPrimitive(_result_)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *false*, return ? AsyncIteratorClose(_iterated_, NormalCompletion(*false*)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype.find">
<h1>AsyncIterator.prototype.find ( _predicate_ )</h1>
<p>This async method performs the following steps when called:</p>
<emu-alg>
1. Let _iterated_ be ? GetIteratorDirect(*this* value).
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
1. Let _counter_ be 0.
1. Repeat,
1. Let _next_ be ? Await(? IteratorNext(_iterated_)).
1. If ? IteratorComplete(_next_) is *true*, return *undefined*.
1. Let _value_ be ? IteratorValue(_next_).
1. Let _result_ be Completion(Call(_predicate_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. Set _result_ to Completion(AwaitNonPrimitive(_result_)).
1. IfAbruptCloseAsyncIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *true*, return ? AsyncIteratorClose(_iterated_, NormalCompletion(_value_)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asynciteratorprototype-@@tostringtag">
<h1>AsyncIterator.prototype [ @@toStringTag ]</h1>
<p>The initial value of the @@toStringTag property is the String value *"Async Iterator"*.</p>
<emu-note>
<p>Unlike the @@toStringTag on most built-in classes, for web-compatibility reasons this property must be writable.</p>
</emu-note>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-new-asyncgenerator-aos">
<h1>New AsyncGenerator AOs</h1>
<p>These factor out functionality from AsyncGenerator.prototype.next and AsyncGenerator.prototype.return and should be used in those methods when this proposal lands in the main specification.</p>
<emu-clause id="sec-asyncgenerator-next" type="abstract operation">
<h1>
AsyncGeneratorNext (
_generator_: unknown,
_brand_: unknown,
_value_: an ECMAScript language value,
): an ECMAScript language value
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Let _result_ be Completion(AsyncGeneratorValidate(_generator_, _brand_)).
1. IfAbruptRejectPromise(_result_, _promiseCapability_).
1. Let _state_ be _generator_.[[AsyncGeneratorState]].
1. If _state_ is ~completed~, then
1. Let _iteratorResult_ be CreateIterResultObject(*undefined*, *true*).
1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _iteratorResult_ »).
1. Return _promiseCapability_.[[Promise]].
1. Let _completion_ be NormalCompletion(_value_).
1. Perform AsyncGeneratorEnqueue(_generator_, _completion_, _promiseCapability_).
1. If _state_ is either ~suspendedStart~ or ~suspendedYield~, then
1. Perform AsyncGeneratorResume(_generator_, _completion_).
1. Else,
1. Assert: _state_ is either ~executing~ or ~awaiting-return~.
1. Return _promiseCapability_.[[Promise]].
</emu-alg>
</emu-clause>
<emu-clause id="sec-asyncgenerator-return" type="abstract operation">
<h1>
AsyncGeneratorReturn (
_generator_: unknown,
_brand_: unknown,
_value_: an ECMAScript language value,
): an ECMAScript language value
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Let _result_ be Completion(AsyncGeneratorValidate(_generator_, _brand_)).
1. IfAbruptRejectPromise(_result_, _promiseCapability_).
1. Let _completion_ be Completion Record { [[Type]]: ~return~, [[Value]]: _value_, [[Target]]: ~empty~ }.
1. Perform AsyncGeneratorEnqueue(_generator_, _completion_, _promiseCapability_).
1. Let _state_ be _generator_.[[AsyncGeneratorState]].
1. If _state_ is either ~suspendedStart~ or ~completed~, then
1. Set _generator_.[[AsyncGeneratorState]] to ~awaiting-return~.
1. Perform ! AsyncGeneratorAwaitReturn(_generator_).
1. Else if _state_ is ~suspendedYield~, then
1. Perform AsyncGeneratorResume(_generator_, _completion_).
1. Else,
1. Assert: _state_ is either ~executing~ or ~awaiting-return~.
1. Return _promiseCapability_.[[Promise]].
</emu-alg>
</emu-clause>
</emu-clause>