Skip to content

Commit e1b629d

Browse files
committed
fix: fix tests and add some comments
1 parent c080ffe commit e1b629d

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

lib/cursor/queryCursor.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ function QueryCursor(query) {
8383
// Max out the number of documents we'll populate in parallel at 5000.
8484
this.options._populateBatchSize = Math.min(this.options.batchSize, 5000);
8585
}
86+
if (query._mongooseOptions._asyncIterator) {
87+
this._mongooseOptions._asyncIterator = true;
88+
}
8689

8790
if (model.collection._shouldBufferCommands() && model.collection.buffer) {
8891
model.collection.queue.push([
@@ -410,7 +413,8 @@ QueryCursor.prototype.addCursorFlag = function(flag, value) {
410413
*/
411414

412415
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
414418
this._mongooseOptions._asyncIterator = true;
415419
return this;
416420
};
@@ -433,7 +437,7 @@ function _next(ctx, cb) {
433437
return cb(err);
434438
}
435439

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
437441
// skip. In either case, avoid transforms.
438442
if (doc === null) {
439443
if (ctx._mongooseOptions._asyncIterator) {
@@ -450,7 +454,8 @@ function _next(ctx, cb) {
450454
}, doc);
451455
}
452456

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
454459
if (ctx._mongooseOptions._asyncIterator) {
455460
return cb(null, { value: doc, done: false });
456461
}

lib/query.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5476,8 +5476,10 @@ Query.prototype.nearSphere = function() {
54765476
*/
54775477

54785478
if (Symbol.asyncIterator != null) {
5479-
Query.prototype[Symbol.asyncIterator] = function() {
5480-
return this.cursor().transformNull()._transformForAsyncIterator();
5479+
Query.prototype[Symbol.asyncIterator] = function queryAsyncIterator() {
5480+
// Set so QueryCursor knows it should transform results for async iterators into `{ value, done }` syntax
5481+
this._mongooseOptions._asyncIterator = true;
5482+
return this.cursor();
54815483
};
54825484
}
54835485

0 commit comments

Comments
 (0)