Skip to content

Commit

Permalink
stream: dont wait for next item in take when finished
Browse files Browse the repository at this point in the history
PR-URL: #47132
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Erick Wendel <erick.workspace@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
  • Loading branch information
rluvaton authored and RafaelGSS committed Apr 7, 2023
1 parent 78ce8d3 commit f378256
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/internal/streams/operators.js
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
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());
}

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

0 comments on commit f378256

Please sign in to comment.