-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
eachAsync not waiting for all promises when parallel and populate is used #8352
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
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
Here's a repro script for this one: #!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
const { Schema, connection} = mongoose;
const DB = '8352';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { useNewUrlParser: true, useUnifiedTopology: true };
const schema = new Schema({
name: String
});
const otherSchema = new Schema({
tests: [Schema.Types.ObjectId]
});
const Test = mongoose.model('test', schema);
const Other = mongoose.model('other', otherSchema);
function makeTests() {
return Array.from({ length: 10 }).map((_, i) => {
return new Test({
name: `name${i}`
});
});
}
const tests1 = makeTests();
const tests2 = makeTests();
const other1 = new Other({ others: tests1.map(t => t._id) });
const other2 = new Other({ others: tests2.map(t => t._id) });
async function run() {
await mongoose.connect(URI, OPTS);
await connection.dropDatabase();
await Test.create(tests1.concat(tests2));
await Other.create([other1, other2]);
const others = await Other.find({})
.populate('tests')
.lean()
.limit(10)
.cursor();
await others.eachAsync(
async () => {
console.log('a');
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log('b');
}
, { parallel: 10 });
console.log('done');
await connection.close();
}
run(); output:
|
Thanks a lot :) |
No worries, thanks for pointing out this issue! Clearly our tests weren't quite rigorous enough. |
This was referenced Mar 15, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
when using eachAsync with parallel option set to 10 and having a populate on the query, mongoose returns even before all awaits are finished.
see test script, output:
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
eachAsync needs to wait for all promises
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
mongoose 5.7.11
node v12.13.0
mongodb 4.2.1
The text was updated successfully, but these errors were encountered: