Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(QueryCursor): eachAsync parameter of AggregationCursor and QueryCursor #14299

Merged
merged 7 commits into from Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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