@@ -83,6 +83,9 @@ function QueryCursor(query) {
83
83
// Max out the number of documents we'll populate in parallel at 5000.
84
84
this . options . _populateBatchSize = Math . min ( this . options . batchSize , 5000 ) ;
85
85
}
86
+ if ( query . _mongooseOptions . _asyncIterator ) {
87
+ this . _mongooseOptions . _asyncIterator = true ;
88
+ }
86
89
87
90
if ( model . collection . _shouldBufferCommands ( ) && model . collection . buffer ) {
88
91
model . collection . queue . push ( [
@@ -410,7 +413,8 @@ QueryCursor.prototype.addCursorFlag = function(flag, value) {
410
413
*/
411
414
412
415
if ( Symbol . asyncIterator != null ) {
413
- QueryCursor . prototype [ Symbol . asyncIterator ] = function ( ) {
416
+ QueryCursor . prototype [ Symbol . asyncIterator ] = function queryCursorAsyncIterator ( ) {
417
+ // Set so QueryCursor knows it should transform results for async iterators into `{ value, done }` syntax
414
418
this . _mongooseOptions . _asyncIterator = true ;
415
419
return this ;
416
420
} ;
@@ -433,7 +437,7 @@ function _next(ctx, cb) {
433
437
return cb ( err ) ;
434
438
}
435
439
436
- // Handle null documents - if asyncIterator, we need `done: true`, otherwise just
440
+ // Handle null documents - if asyncIterator, we need to return `done: true`, otherwise just
437
441
// skip. In either case, avoid transforms.
438
442
if ( doc === null ) {
439
443
if ( ctx . _mongooseOptions . _asyncIterator ) {
@@ -450,7 +454,8 @@ function _next(ctx, cb) {
450
454
} , doc ) ;
451
455
}
452
456
453
- // For async iterator, convert to {value, done} format
457
+ // This option is set in `Symbol.asyncIterator` code paths.
458
+ // For async iterator, we need to convert to {value, done} format
454
459
if ( ctx . _mongooseOptions . _asyncIterator ) {
455
460
return cb ( null , { value : doc , done : false } ) ;
456
461
}
0 commit comments