From dffd3449a03a4706938e1b037f5828787998ea12 Mon Sep 17 00:00:00 2001 From: Paul Taylor <178183+trxcllnt@users.noreply.github.com> Date: Fri, 17 May 2024 20:40:28 -0700 Subject: [PATCH] fix(flatmap): flatMap shouldn't throw when the input size is smaller than concurrent (#367) fix #366 --- spec/asynciterable-operators/flatmap-spec.ts | 7 +++++++ src/util/returniterator.ts | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/spec/asynciterable-operators/flatmap-spec.ts b/spec/asynciterable-operators/flatmap-spec.ts index e3fccb02..c5890530 100644 --- a/spec/asynciterable-operators/flatmap-spec.ts +++ b/spec/asynciterable-operators/flatmap-spec.ts @@ -9,6 +9,13 @@ test('AsyncIterable#flatMap with range', async () => { expect(await toArray(ys)).toEqual([0, 0, 0, 1, 1, 2]); }); +test(`AsyncIterable#flatMap with fewer inputs than max concurrent doesn't throw`, async () => { + const xs = of(1, 2); + const ys = xs.pipe(flatMap(async (x) => range(0, x), 3)); + + expect(await toArray(ys)).toEqual([0, 0, 1]); +}); + test('AsyncIterable#flatMap selector returns throw', async () => { const err = new Error(); const xs = of(1, 2, 3); diff --git a/src/util/returniterator.ts b/src/util/returniterator.ts index 8e1836ad..6f34b9a5 100644 --- a/src/util/returniterator.ts +++ b/src/util/returniterator.ts @@ -2,7 +2,7 @@ * @ignore */ export function returnIterator(it: Iterator) { - if (typeof it.return === 'function') { + if (typeof it?.return === 'function') { it.return(); } } @@ -11,7 +11,7 @@ export function returnIterator(it: Iterator) { * @ignore */ export async function returnAsyncIterator(it: AsyncIterator): Promise { - if (typeof it.return === 'function') { + if (typeof it?.return === 'function') { await it.return(); } }