@@ -11,45 +11,41 @@ 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
+ // istanbul ignore else (FIXME: add test case)
25
+ if ( typeof iterator . return === 'function' ) {
26
+ try {
27
+ await iterator . return ( ) ;
28
+ } catch ( _e ) {
29
+ /* ignore error */
30
+ }
31
+ }
32
+ throw error ;
36
33
}
37
34
}
38
35
39
36
return {
40
- next ( ) : Promise < IteratorResult < U , void > > {
41
- return mapResult ( iterator . next( ) ) ;
37
+ async next ( ) {
38
+ return mapResult ( await iterator . next ( ) ) ;
42
39
} ,
43
- return ( ) : Promise < IteratorResult < U , void >> {
40
+ async return ( ) : Promise < IteratorResult < U , void > > {
44
41
return typeof iterator . return === 'function'
45
- ? mapResult ( iterator . return ( ) )
46
- : Promise . resolve ( { value : undefined , done : true } ) ;
42
+ ? mapResult ( await iterator . return ( ) )
43
+ : { value : undefined , done : true } ;
47
44
} ,
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 ) ;
45
+ async throw ( error ?: mixed ) {
46
+ return typeof iterator . throw === 'function'
47
+ ? mapResult ( await iterator . throw ( error ) )
48
+ : Promise . reject ( error ) ;
53
49
} ,
54
50
[ Symbol . asyncIterator ] ( ) {
55
51
return this ;
0 commit comments