File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -21,10 +21,14 @@ function callbackToAsyncIterator<CallbackInput: any, ReturnVal: any>(
21
21
let pushQueue = [ ] ;
22
22
let listening = true ;
23
23
let listenerReturnValue ;
24
+ let listenerReturnedValue = false ;
25
+ let closingWaitingOnListenerReturnValue = false ;
24
26
// Start listener
25
27
listener ( value => pushValue ( value ) )
26
28
. then ( a => {
27
29
listenerReturnValue = a ;
30
+ listenerReturnedValue = true ;
31
+ if ( closingWaitingOnListenerReturnValue ) emptyQueue ( ) ;
28
32
} )
29
33
. catch ( err => {
30
34
onError ( err ) ;
@@ -49,6 +53,10 @@ function callbackToAsyncIterator<CallbackInput: any, ReturnVal: any>(
49
53
}
50
54
51
55
function emptyQueue ( ) {
56
+ if ( onClose && ! listenerReturnedValue ) {
57
+ closingWaitingOnListenerReturnValue = true ;
58
+ return ;
59
+ }
52
60
if ( listening ) {
53
61
listening = false ;
54
62
pullQueue . forEach ( resolve => resolve ( { value : undefined , done : true } ) ) ;
Original file line number Diff line number Diff line change @@ -145,6 +145,24 @@ describe('options', () => {
145
145
await iter . return ( ) ;
146
146
} ) ;
147
147
148
+ it ( 'should call onClose with the return value from an listener only after the promise resolves' , async ( ) => {
149
+ const returnValue = 'asdf' ;
150
+ const listener = ( cb : ( ) = > void ) =>
151
+ new Promise ( res => {
152
+ res ( returnValue ) ;
153
+ } ) ;
154
+
155
+ expect . hasAssertions ( ) ;
156
+ const iter = asyncify ( listener , {
157
+ onClose : val => {
158
+ expect ( val ) . toEqual ( returnValue ) ;
159
+ } ,
160
+ } ) ;
161
+ // Wait a tick so that the promise resolves with the return value
162
+ iter . return ( ) ;
163
+ await new Promise ( res => setTimeout ( res , 10 ) ) ;
164
+ } ) ;
165
+
148
166
describe ( 'buffering' , ( ) => {
149
167
it ( 'should not buffer incoming values if disabled' , async ( ) => {
150
168
const listener = ( cb : ( arg : number ) = > void ) =>
You can’t perform that action at this time.
0 commit comments