GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: dont wait for next item in take when finished · nodejs/node@f378256

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/streams
    • operators.js
  • test/parallel
    • test-stream-drop-take.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/streams/operators.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/operators.js
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,10 @@ function take(number, options = undefined) {
409409
}
410410
if (number-- > 0) {
411411
yield val;
412-
} else {
412+
}
413+
414+
// Don't get another item from iterator in case we reached the end
415+
if (number <= 0) {
413416
return;
414417
}
415418
}
Collapse file

‎test/parallel/test-stream-drop-take.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-stream-drop-take.js
+23-1Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const common = require('../common');
44
const {
55
Readable,
66
} = require('stream');
7-
const { deepStrictEqual, rejects, throws } = require('assert');
7+
const { deepStrictEqual, rejects, throws, strictEqual } = require('assert');
88

99
const { from } = Readable;
1010

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

52+
53+
// Don't wait for next item in the original stream when already consumed the requested take amount
54+
{
55+
let reached = false;
56+
let resolve;
57+
const promise = new Promise((res) => resolve = res);
58+
59+
const stream = from((async function *() {
60+
yield 1;
61+
await promise;
62+
reached = true;
63+
yield 2;
64+
})());
65+
66+
stream.take(1)
67+
.toArray()
68+
.then(common.mustCall(() => {
69+
strictEqual(reached, false);
70+
}))
71+
.finally(() => resolve());
72+
}
73+
5274
{
5375
// Coercion
5476
(async () => {

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.