Skip to content

Commit

Permalink
Merge pull request #14299 from DevooKim/cursor
Browse files Browse the repository at this point in the history
fix(QueryCursor): eachAsync parameter of AggregationCursor and QueryCursor
  • Loading branch information
vkarpov15 committed Jan 26, 2024
2 parents 33c624e + 827e6ad commit 8ef0d9a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
3 changes: 3 additions & 0 deletions docs/migrating_to_7.md
Expand Up @@ -87,6 +87,8 @@ They always return promises.
* `Aggregate.prototype.exec`
* `Aggregate.prototype.explain`
* `AggregationCursor.prototype.close`
* `AggregationCursor.prototype.next`
* `AggregationCursor.prototype.eachAsync`
* `Connection.prototype.startSession`
* `Connection.prototype.dropCollection`
* `Connection.prototype.createCollection`
Expand Down Expand Up @@ -138,6 +140,7 @@ They always return promises.
* `Query.prototype.exec`
* `QueryCursor.prototype.close`
* `QueryCursor.prototype.next`
* `QueryCursor.prototype.eachAsync`

If you are using the above functions with callbacks, we recommend switching to async/await, or promises if async functions don't work for you.
If you need help refactoring a legacy codebase, [this tool from Mastering JS callbacks to async await](https://masteringjs.io/tutorials/tools/callback-to-async-await) using ChatGPT.
Expand Down
11 changes: 7 additions & 4 deletions lib/cursor/aggregationCursor.js
Expand Up @@ -219,21 +219,24 @@ AggregationCursor.prototype.next = async function next() {
* @param {Function} fn
* @param {Object} [options]
* @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1.
* @param {Function} [callback] executed when all docs have been processed
* @param {Number} [options.batchSize=null] if set, Mongoose will call `fn` with an array of at most `batchSize` documents, instead of a single document
* @param {Boolean} [options.continueOnError=false] if true, `eachAsync()` iterates through all docs even if `fn` throws an error. If false, `eachAsync()` throws an error immediately if the given function `fn()` throws an error.
* @return {Promise}
* @api public
* @method eachAsync
*/

AggregationCursor.prototype.eachAsync = function(fn, opts, callback) {
AggregationCursor.prototype.eachAsync = function(fn, opts) {
if (typeof arguments[2] === 'function') {
throw new MongooseError('AggregationCursor.prototype.eachAsync() no longer accepts a callback');
}
const _this = this;
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = opts || {};

return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts, callback);
return eachAsync(function(cb) { return _next(_this, cb); }, fn, opts);
};

/**
Expand Down
11 changes: 6 additions & 5 deletions lib/cursor/queryCursor.js
Expand Up @@ -243,7 +243,7 @@ QueryCursor.prototype.rewind = function() {
*/

QueryCursor.prototype.next = async function next() {
if (arguments[0] === 'function') {
if (typeof arguments[0] === 'function') {
throw new MongooseError('QueryCursor.prototype.next() no longer accepts a callback');
}
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -277,20 +277,21 @@ QueryCursor.prototype.next = async function next() {
* @param {Number} [options.parallel] the number of promises to execute in parallel. Defaults to 1.
* @param {Number} [options.batchSize] if set, will call `fn()` with arrays of documents with length at most `batchSize`
* @param {Boolean} [options.continueOnError=false] if true, `eachAsync()` iterates through all docs even if `fn` throws an error. If false, `eachAsync()` throws an error immediately if the given function `fn()` throws an error.
* @param {Function} [callback] executed when all docs have been processed
* @return {Promise}
* @api public
* @method eachAsync
*/

QueryCursor.prototype.eachAsync = function(fn, opts, callback) {
QueryCursor.prototype.eachAsync = function(fn, opts) {
if (typeof arguments[2] === 'function') {
throw new MongooseError('QueryCursor.prototype.eachAsync() no longer accepts a callback');
}
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = opts || {};

return eachAsync((cb) => _next(this, cb), fn, opts, callback);
return eachAsync((cb) => _next(this, cb), fn, opts);
};

/**
Expand Down

0 comments on commit 8ef0d9a

Please sign in to comment.