| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e0a00eb commit 4edc1ab
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -409,7 +409,10 @@ function take(number, options = undefined) { | |||
| 409 | 409 | } | |
| 410 | 410 | if (number-- > 0) { | |
| 411 | 411 | yield val; | |
| 412 | - } else { | ||
| 412 | + } | ||
| 413 | + | ||
| 414 | + // Don't get another item from iterator in case we reached the end | ||
| 415 | + if (number <= 0) { | ||
| 413 | 416 | return; | |
| 414 | 417 | } | |
| 415 | 418 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ const common = require('../common'); | |||
| 4 | 4 | const { | |
| 5 | 5 | Readable, | |
| 6 | 6 | } = require('stream'); | |
| 7 | - const { deepStrictEqual, rejects, throws } = require('assert'); | ||
| 7 | + const { deepStrictEqual, rejects, throws, strictEqual } = require('assert'); | ||
| 8 | 8 | ||
| 9 | 9 | const { from } = Readable; | |
| 10 | 10 | ||
@@ -49,6 +49,28 @@ const naturals = () => from(async function*() { | |||
| 49 | 49 | })().then(common.mustCall()); | |
| 50 | 50 | } | |
| 51 | 51 | ||
| 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 | + | ||
| 52 | 74 | { | |
| 53 | 75 | // Coercion | |
| 54 | 76 | (async () => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments