Skip to content

eachAsync does not wait for promises when parallel is greater than the number of records in the cursor #8422

Closed
@rrglomsk

Description

@rrglomsk

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
For eachAsync, if the 'parallel' option is set to a higher number than the number of records in the cursor, eachAsync will move on without waiting for functions to resolve. (For example, when the number of records in the cursor is 3 and the options are set to { parallel: 4 })

This does not appear to be resolved by the eachAsync fixes made in 5.7.14.

If the current behavior is a bug, please provide the steps to reproduce.

Here's a failing test to demonstrate- I just borrowed the setup from test/es-next/asyncIterator.test.es6.js:

'use strict';

const assert = require('assert');
const start = require('../common');

const mongoose = start.mongoose;

describe('eachAsync', function() {
  let db;
  let Movie;

  before(async function() {
    db = await start();

    const schema = new mongoose.Schema({ name: String });
    Movie = db.model('gh6737_Movie', schema);

    await Movie.create([
      { name: 'Kickboxer' },
      { name: 'Ip Man' },
      { name: 'Enter the Dragon' }
    ]);
  });

  after(function(done) {
    db.close(done);
  });

  it('waits for all promises to resolve when the parallel setting surpasses the number of records in the cursor', async function() {
    var a = false;

    async function test() {
      await new Promise((resolve) => setTimeout(resolve, 100));
      a = true;
    }

    await Movie.find().cursor().eachAsync(test, { parallel: 4 })
    assert.equal(a, true);
  });

Note this test will pass when parallel is less than or equal to 3.

What is the expected behavior?
eachAsync will wait for all functions to resolve, regardless of number of records in the cursor and what the parallel count is set to.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 12.13.1
Mongoose: 5.7.14
MongoDB: 4.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions