QueryCursor asyncIterator may not work if the iteration body is asynchronous #9403
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Do you want to request a feature or report a bug?
bug
What is the current behavior?
Sample script: https://gist.github.com/dbellavista/f9523dc900a3e93a02d4027295897499
the
QueryCursor
inherit fromstream.Readable
the[Symbol.asyncIterable]
method (see https://nodejs.org/api/stream.html#stream_readable_symbol_asynciterator), allowing it to be used inside the for await or as an async iterable.Which basically invokes continuously the
_read
method until the internal buffer is filled. The problem is that when the body of thefor await
is asyncronous. The_read
method closes the cursor and emits the close eventmongoose/lib/cursor/QueryCursor.js
Lines 90 to 96 in 1729a8b
I think the Readable stream doesn't like this behaviour and stops the async iteration. By increasing the cursor setTimeout from 0 to 1000 the sample script executes correctly.
What happen is:
iterable.next()
Readable._read
is invoked and the next value is served to the iteration bodyReadable._read
is invoked in "background" until the buffer is fullemit
event is forcefully invokediterable.next()
againReadable
stream has been closed so the iteration is overWhat are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
The text was updated successfully, but these errors were encountered: