@@ -52,42 +52,34 @@ export function isAsyncIterable(
52
52
) ;
53
53
}
54
54
55
- function asyncIterableToPromise < T > (
55
+ async function asyncIterableToPromise < T > (
56
56
input : AsyncIterable < T > | AsyncIterableIterator < T > ,
57
57
) : Promise < T > {
58
- return new Promise ( ( resolve , reject ) => {
59
- // Also support AsyncGenerator on Safari iOS.
60
- // As mentioned in the isAsyncIterable function there is no Symbol.asyncIterator available
61
- // so every AsyncIterable must be implemented using AsyncGenerator.
62
- const iteratorReturn = (
63
- 'return' in input ? input : input [ Symbol . asyncIterator ] ( )
64
- ) . return ?. bind ( input ) ;
65
- const iteratorNext = (
66
- 'next' in input ? input : input [ Symbol . asyncIterator ] ( )
67
- ) . next . bind ( input ) ;
58
+ // Also support AsyncGenerator on Safari iOS.
59
+ // As mentioned in the isAsyncIterable function, there is no Symbol.asyncIterator available,
60
+ // so every AsyncIterable must be implemented using AsyncGenerator.
61
+ const iteratorReturn = (
62
+ 'return' in input ? input : input [ Symbol . asyncIterator ] ( )
63
+ ) . return ?. bind ( input ) ;
64
+ const iteratorNext = (
65
+ 'next' in input ? input : input [ Symbol . asyncIterator ] ( )
66
+ ) . next . bind ( input ) ;
68
67
69
- iteratorNext ( )
70
- . then ( result => {
71
- resolve ( result . value ) ;
72
- // ensure cleanup
73
- void iteratorReturn ?.( ) ;
74
- } )
75
- . catch ( err => {
76
- reject ( err ) ;
77
- } ) ;
78
- } ) ;
68
+ const result = await iteratorNext ( ) ;
69
+ // ensure cleanup
70
+ void iteratorReturn ?.( ) ;
71
+ return result . value ;
79
72
}
80
73
81
- export function fetcherReturnToPromise (
74
+ export async function fetcherReturnToPromise (
82
75
fetcherResult : FetcherReturnType ,
83
76
) : Promise < FetcherResult > {
84
- return Promise . resolve ( fetcherResult ) . then ( result => {
85
- if ( isAsyncIterable ( result ) ) {
86
- return asyncIterableToPromise ( result ) ;
87
- }
88
- if ( isObservable ( result ) ) {
89
- return observableToPromise ( result ) ;
90
- }
91
- return result ;
92
- } ) ;
77
+ const result = await fetcherResult ;
78
+ if ( isAsyncIterable ( result ) ) {
79
+ return asyncIterableToPromise ( result ) ;
80
+ }
81
+ if ( isObservable ( result ) ) {
82
+ return observableToPromise ( result ) ;
83
+ }
84
+ return result ;
93
85
}
0 commit comments