@@ -11,45 +11,40 @@ export function mapAsyncIterator<T, U>(
11
11
// $FlowIssue[incompatible-use]
12
12
const iterator = iterable [ Symbol . asyncIterator ] ( ) ;
13
13
14
- async function abruptClose ( error : mixed ) {
15
- if ( typeof iterator . return === 'function' ) {
16
- try {
17
- await iterator . return ( ) ;
18
- } catch ( _e ) {
19
- /* ignore error */
20
- }
14
+ async function mapResult (
15
+ result : IteratorResult < T , void > ,
16
+ ) : Promise < IteratorResult < U , void > > {
17
+ if ( result . done ) {
18
+ return result ;
21
19
}
22
- throw error ;
23
- }
24
20
25
- async function mapResult ( resultPromise : Promise < IteratorResult < T , void >> ) {
26
21
try {
27
- const result = await resultPromise ;
28
-
29
- if ( result . done ) {
30
- return result ;
31
- }
32
-
33
22
return { value : await callback ( result . value ) , done : false } ;
34
- } catch ( callbackError ) {
35
- return abruptClose ( callbackError ) ;
23
+ } catch ( error ) {
24
+ if ( typeof iterator . return === 'function' ) {
25
+ try {
26
+ await iterator . return ( ) ;
27
+ } catch ( _e ) {
28
+ /* ignore error */
29
+ }
30
+ }
31
+ throw error ;
36
32
}
37
33
}
38
34
39
35
return {
40
- next ( ) : Promise < IteratorResult < U , void > > {
41
- return mapResult ( iterator . next( ) ) ;
36
+ async next ( ) {
37
+ return mapResult ( await iterator . next ( ) ) ;
42
38
} ,
43
- return ( ) : Promise < IteratorResult < U , void >> {
39
+ async return ( ) : Promise < IteratorResult < U , void > > {
44
40
return typeof iterator . return === 'function'
45
- ? mapResult ( iterator . return ( ) )
46
- : Promise . resolve ( { value : undefined , done : true } ) ;
41
+ ? mapResult ( await iterator . return ( ) )
42
+ : { value : undefined , done : true } ;
47
43
} ,
48
- throw ( error ? : mixed ) : Promise < IteratorResult < U , void >> {
49
- if ( typeof iterator . throw === 'function' ) {
50
- return mapResult ( iterator . throw ( error ) ) ;
51
- }
52
- return Promise . reject ( error ) . catch ( abruptClose ) ;
44
+ async throw ( error ?: mixed ) {
45
+ return typeof iterator . throw === 'function'
46
+ ? mapResult ( await iterator . throw ( error ) )
47
+ : Promise . reject ( error ) ;
53
48
} ,
54
49
[ Symbol . asyncIterator ] ( ) {
55
50
return this ;
0 commit comments