@@ -218,7 +218,7 @@ module.exports = function duplexify(body, name) {
218
218
} ;
219
219
220
220
function fromAsyncGen ( fn , destructor ) {
221
- let { promise, resolve } = PromiseWithResolvers ( ) ;
221
+ let { promise, resolve, reject } = PromiseWithResolvers ( ) ;
222
222
const ac = new AbortController ( ) ;
223
223
const signal = ac . signal ;
224
224
@@ -231,7 +231,7 @@ function fromAsyncGen(fn, destructor) {
231
231
if ( done ) return ;
232
232
if ( signal . aborted )
233
233
throw new AbortError ( undefined , { cause : signal . reason } ) ;
234
- ( { promise, resolve } = PromiseWithResolvers ( ) ) ;
234
+ ( { promise, resolve, reject } = PromiseWithResolvers ( ) ) ;
235
235
// Next line will "break" the loop if the generator is returned/thrown.
236
236
yield chunk ;
237
237
}
@@ -242,6 +242,13 @@ function fromAsyncGen(fn, destructor) {
242
242
try {
243
243
return await originalReturn . call ( this , value ) ;
244
244
} finally {
245
+ if ( resolve ) {
246
+ const _resolve = resolve ;
247
+ resolve = null ;
248
+ reject = null ;
249
+ _resolve ( { done : true , cb : ( ) => { } } ) ;
250
+ }
251
+
245
252
if ( promise ) {
246
253
const _promise = promise ;
247
254
promise = null ;
@@ -258,13 +265,22 @@ function fromAsyncGen(fn, destructor) {
258
265
try {
259
266
return await originalThrow . call ( this , err ) ;
260
267
} finally {
268
+ // asyncGenerator.throw(undefined) should cause a callback error
269
+ const error = err || new AbortError ( ) ;
270
+
271
+ if ( reject ) {
272
+ const _reject = reject ;
273
+ reject = null ;
274
+ resolve = null ;
275
+ _reject ( error ) ;
276
+ }
277
+
261
278
if ( promise ) {
262
279
const _promise = promise ;
263
280
promise = null ;
264
281
const { cb } = await _promise ;
265
282
266
- // asyncGenerator.throw(undefined) should cause a callback error
267
- process . nextTick ( cb , err ?? new AbortError ( ) ) ;
283
+ process . nextTick ( cb , error ) ;
268
284
}
269
285
}
270
286
} ;
@@ -274,14 +290,24 @@ function fromAsyncGen(fn, destructor) {
274
290
return {
275
291
value,
276
292
write ( chunk , encoding , cb ) {
277
- const _resolve = resolve ;
278
- resolve = null ;
279
- _resolve ( { chunk, done : false , cb } ) ;
293
+ if ( resolve ) {
294
+ const _resolve = resolve ;
295
+ resolve = null ;
296
+ reject = null ;
297
+ _resolve ( { chunk, done : false , cb } ) ;
298
+ } else {
299
+ cb ( new AbortError ( ) ) ;
300
+ }
280
301
} ,
281
302
final ( cb ) {
282
- const _resolve = resolve ;
283
- resolve = null ;
284
- _resolve ( { done : true , cb } ) ;
303
+ if ( resolve ) {
304
+ const _resolve = resolve ;
305
+ resolve = null ;
306
+ reject = null ;
307
+ _resolve ( { done : true , cb } ) ;
308
+ } else {
309
+ cb ( new AbortError ( ) ) ;
310
+ }
285
311
} ,
286
312
destroy ( err , cb ) {
287
313
ac . abort ( ) ;
0 commit comments