11import type { MaybePromise } from './executor.js' ;
2- import { isPromise } from './jsutils.js' ;
2+ import { fakePromise , fakeRejectPromise } from './fakePromise.js' ;
3+ import { mapMaybePromise } from './map-maybe-promise.js' ;
34
45/**
56 * Given an AsyncIterable and a callback function, return an AsyncIterator
@@ -21,18 +22,17 @@ export function mapAsyncIterator<T, U>(
2122 if ( onEnd ) {
2223 let onEndWithValueResult : any /** R in onEndWithValue */ ;
2324 onEndWithValue = value => {
24- if ( onEndWithValueResult ) {
25- return onEndWithValueResult ;
26- }
27- const onEnd$ = onEnd ( ) ;
28- return ( onEndWithValueResult = isPromise ( onEnd$ ) ? onEnd$ . then ( ( ) => value ) : value ) ;
25+ onEndWithValueResult ||= mapMaybePromise ( onEnd ( ) , ( ) => value ) ;
26+ return onEndWithValueResult ;
2927 } ;
3028 }
3129
3230 if ( typeof iterator . return === 'function' ) {
3331 $return = iterator . return ;
3432 abruptClose = ( error : any ) => {
35- const rethrow = ( ) => Promise . reject ( error ) ;
33+ const rethrow = ( ) => {
34+ throw error ;
35+ } ;
3636 return $return . call ( iterator ) . then ( rethrow , rethrow ) ;
3737 } ;
3838 }
@@ -41,7 +41,9 @@ export function mapAsyncIterator<T, U>(
4141 if ( result . done ) {
4242 return onEndWithValue ? onEndWithValue ( result ) : result ;
4343 }
44- return asyncMapValue ( result . value , onNext ) . then ( iteratorResult , abruptClose ) ;
44+ return mapMaybePromise ( result . value , value =>
45+ mapMaybePromise ( onNext ( value ) , iteratorResult , abruptClose ) ,
46+ ) ;
4547 }
4648
4749 let mapReject : any ;
@@ -50,10 +52,10 @@ export function mapAsyncIterator<T, U>(
5052 // Capture rejectCallback to ensure it cannot be null.
5153 const reject = onError ;
5254 mapReject = ( error : any ) => {
53- if ( onErrorResult ) {
54- return onErrorResult ;
55- }
56- return ( onErrorResult = asyncMapValue ( error , reject ) . then ( iteratorResult , abruptClose ) ) ;
55+ onErrorResult ||= mapMaybePromise ( error , error =>
56+ mapMaybePromise ( reject ( error ) , iteratorResult , abruptClose ) ,
57+ ) ;
58+ return onErrorResult ;
5759 } ;
5860 }
5961
@@ -64,25 +66,24 @@ export function mapAsyncIterator<T, U>(
6466 return ( ) {
6567 const res$ = $return
6668 ? $return . call ( iterator ) . then ( mapResult , mapReject )
67- : Promise . resolve ( { value : undefined , done : true } ) ;
69+ : fakePromise ( { value : undefined , done : true } ) ;
6870 return onEndWithValue ? res$ . then ( onEndWithValue ) : res$ ;
6971 } ,
7072 throw ( error : any ) {
7173 if ( typeof iterator . throw === 'function' ) {
7274 return iterator . throw ( error ) . then ( mapResult , mapReject ) ;
7375 }
74- return Promise . reject ( error ) . catch ( abruptClose ) ;
76+ if ( abruptClose ) {
77+ return abruptClose ( error ) ;
78+ }
79+ return fakeRejectPromise ( error ) ;
7580 } ,
7681 [ Symbol . asyncIterator ] ( ) {
7782 return this ;
7883 } ,
7984 } ;
8085}
8186
82- function asyncMapValue < T , U > ( value : T , callback : ( value : T ) => PromiseLike < U > | U ) : Promise < U > {
83- return new Promise ( resolve => resolve ( callback ( value ) ) ) ;
84- }
85-
8687function iteratorResult < T > ( value : T ) : IteratorResult < T > {
8788 return { value, done : false } ;
8889}
0 commit comments