diff --git a/lib/internal/streams/operators.js b/lib/internal/streams/operators.js index 9a997511f5ed97..47cba0cc732c82 100644 --- a/lib/internal/streams/operators.js +++ b/lib/internal/streams/operators.js @@ -232,10 +232,13 @@ async function toArray(options) { return result; } -async function* flatMap(fn, options) { - for await (const val of this.map(fn, options)) { - yield* val; - } +function flatMap(fn, options) { + const values = this.map(fn, options); + return async function* flatMap() { + for await (const val of values) { + yield* val; + } + }(); } function toIntegerOrInfinity(number) { diff --git a/test/parallel/test-stream-flatMap.js b/test/parallel/test-stream-flatMap.js index 1ace8a0cf513bc..952043b07b35f7 100644 --- a/test/parallel/test-stream-flatMap.js +++ b/test/parallel/test-stream-flatMap.js @@ -109,20 +109,11 @@ function oneTo5() { { // Error cases - assert.rejects(async () => { - // eslint-disable-next-line no-unused-vars - for await (const unused of Readable.from([1]).flatMap(1)); - }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); - assert.rejects(async () => { - // eslint-disable-next-line no-unused-vars - for await (const _ of Readable.from([1]).flatMap((x) => x, { - concurrency: 'Foo' - })); - }, /ERR_OUT_OF_RANGE/).then(common.mustCall()); - assert.rejects(async () => { - // eslint-disable-next-line no-unused-vars - for await (const _ of Readable.from([1]).flatMap((x) => x, 1)); - }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); + assert.throws(() => Readable.from([1]).flatMap(1), /ERR_INVALID_ARG_TYPE/); + assert.throws(() => Readable.from([1]).flatMap((x) => x, { + concurrency: 'Foo' + }), /ERR_OUT_OF_RANGE/); + assert.throws(() => Readable.from([1]).flatMap((x) => x, 1), /ERR_INVALID_ARG_TYPE/); } { // Test result is a Readable