@@ -11,45 +11,41 @@ export function mapAsyncIterator<T, U>(
1111 // $FlowIssue[incompatible-use]
1212 const iterator = iterable [ Symbol . asyncIterator ] ( ) ;
1313
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 ;
2119 }
22- throw error ;
23- }
2420
25- async function mapResult ( resultPromise : Promise < IteratorResult < T , void >> ) {
2621 try {
27- const result = await resultPromise ;
28-
29- if ( result . done ) {
30- return result ;
31- }
32-
3322 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 ;
3633 }
3734 }
3835
3936 return {
40- next ( ) : Promise < IteratorResult < U , void > > {
41- return mapResult ( iterator . next( ) ) ;
37+ async next ( ) {
38+ return mapResult ( await iterator . next ( ) ) ;
4239 } ,
43- return ( ) : Promise < IteratorResult < U , void >> {
40+ async return ( ) : Promise < IteratorResult < U , void > > {
4441 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 } ;
4744 } ,
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 ) ;
5349 } ,
5450 [ Symbol . asyncIterator ] ( ) {
5551 return this ;
0 commit comments