Skip to content

stream: don't wait for next item in take when finished #47132

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/internal/streams/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ function take(number, options = undefined) {
}
if (number-- > 0) {
yield val;
} else {
}

// Don't get another item from iterator in case we reached the end
if (number <= 0) {
return;
}
}
Expand Down
24 changes: 23 additions & 1 deletion test/parallel/test-stream-drop-take.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const common = require('../common');
const {
Readable,
} = require('stream');
const { deepStrictEqual, rejects, throws } = require('assert');
const { deepStrictEqual, rejects, throws, strictEqual } = require('assert');

const { from } = Readable;

Expand Down Expand Up @@ -49,6 +49,28 @@ const naturals = () => from(async function*() {
})().then(common.mustCall());
}


// Don't wait for next item in the original stream when already consumed the requested take amount
{
let reached = false;
let resolve;
const promise = new Promise((res) => resolve = res);

const stream = from((async function *() {
yield 1;
await promise;
reached = true;
yield 2;
})());

stream.take(1)
.toArray()
.then(common.mustCall(() => {
strictEqual(reached, false);
}))
.finally(() => resolve());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to do it. right?
the entire closure will be GCed...

}

{
// Coercion
(async () => {
Expand Down