@@ -428,12 +428,18 @@ export abstract class AbstractCursor<
428
428
return false ;
429
429
}
430
430
431
- do {
432
- if ( ( this . documents ?. length ?? 0 ) !== 0 ) {
433
- return true ;
431
+ try {
432
+ do {
433
+ if ( ( this . documents ?. length ?? 0 ) !== 0 ) {
434
+ return true ;
435
+ }
436
+ await this . fetchBatch ( ) ;
437
+ } while ( ! this . isDead || ( this . documents ?. length ?? 0 ) !== 0 ) ;
438
+ } finally {
439
+ if ( this . cursorOptions . timeoutMode === CursorTimeoutMode . ITERATION && this . cursorId != null ) {
440
+ this . timeoutContext ?. refresh ( ) ;
434
441
}
435
- await this . fetchBatch ( ) ;
436
- } while ( ! this . isDead || ( this . documents ?. length ?? 0 ) !== 0 ) ;
442
+ }
437
443
438
444
return false ;
439
445
}
@@ -444,18 +450,20 @@ export abstract class AbstractCursor<
444
450
throw new MongoCursorExhaustedError ( ) ;
445
451
}
446
452
447
- if ( this . cursorOptions . timeoutMode === CursorTimeoutMode . ITERATION ) {
448
- this . timeoutContext ?. refresh ( ) ;
449
- }
450
-
451
- do {
452
- const doc = this . documents ?. shift ( this . cursorOptions ) ;
453
- if ( doc != null ) {
454
- if ( this . transform != null ) return await this . transformDocument ( doc ) ;
455
- return doc ;
453
+ try {
454
+ do {
455
+ const doc = this . documents ?. shift ( this . cursorOptions ) ;
456
+ if ( doc != null ) {
457
+ if ( this . transform != null ) return await this . transformDocument ( doc ) ;
458
+ return doc ;
459
+ }
460
+ await this . fetchBatch ( ) ;
461
+ } while ( ! this . isDead || ( this . documents ?. length ?? 0 ) !== 0 ) ;
462
+ } finally {
463
+ if ( this . cursorOptions . timeoutMode === CursorTimeoutMode . ITERATION && this . cursorId != null ) {
464
+ this . timeoutContext ?. refresh ( ) ;
456
465
}
457
- await this . fetchBatch ( ) ;
458
- } while ( ! this . isDead || ( this . documents ?. length ?? 0 ) !== 0 ) ;
466
+ }
459
467
460
468
return null ;
461
469
}
@@ -468,18 +476,24 @@ export abstract class AbstractCursor<
468
476
throw new MongoCursorExhaustedError ( ) ;
469
477
}
470
478
471
- let doc = this . documents ?. shift ( this . cursorOptions ) ;
472
- if ( doc != null ) {
473
- if ( this . transform != null ) return await this . transformDocument ( doc ) ;
474
- return doc ;
475
- }
479
+ try {
480
+ let doc = this . documents ?. shift ( this . cursorOptions ) ;
481
+ if ( doc != null ) {
482
+ if ( this . transform != null ) return await this . transformDocument ( doc ) ;
483
+ return doc ;
484
+ }
476
485
477
- await this . fetchBatch ( ) ;
486
+ await this . fetchBatch ( ) ;
478
487
479
- doc = this . documents ?. shift ( this . cursorOptions ) ;
480
- if ( doc != null ) {
481
- if ( this . transform != null ) return await this . transformDocument ( doc ) ;
482
- return doc ;
488
+ doc = this . documents ?. shift ( this . cursorOptions ) ;
489
+ if ( doc != null ) {
490
+ if ( this . transform != null ) return await this . transformDocument ( doc ) ;
491
+ return doc ;
492
+ }
493
+ } finally {
494
+ if ( this . cursorOptions . timeoutMode === CursorTimeoutMode . ITERATION && this . cursorId != null ) {
495
+ this . timeoutContext ?. refresh ( ) ;
496
+ }
483
497
}
484
498
485
499
return null ;
0 commit comments