Skip to content

Commit

Permalink
same fix for flatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
iMoses committed Jan 24, 2022
1 parent 3aadc06 commit f90e4d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
11 changes: 7 additions & 4 deletions lib/internal/streams/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 5 additions & 14 deletions test/parallel/test-stream-flatMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f90e4d9

Please sign in to comment.