Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/subscription/__tests__/mapAsyncIterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,19 @@ describe('mapAsyncIterator', () => {
);
});

async function testClosesSourceWithRejectMapper<T>(mapper: (Error) => T) {
it('closes source if mapper throws an error', async () => {
async function* source() {
yield 1;
throw new Error(2);
}

const throwOver1 = mapAsyncIterator(source(), (x) => x, mapper);
const throwOver1 = mapAsyncIterator(
source(),
(x) => x,
(error) => {
throw new Error('Cannot count to ' + error.message);
},
);

expect(await throwOver1.next()).to.deep.equal({ value: 1, done: false });

Expand All @@ -379,17 +385,5 @@ describe('mapAsyncIterator', () => {
value: undefined,
done: true,
});
}

it('closes source if mapper throws an error', async () => {
await testClosesSourceWithRejectMapper((error) => {
throw new Error('Cannot count to ' + error.message);
});
});

it('closes source if mapper rejects', async () => {
await testClosesSourceWithRejectMapper((error) =>
Promise.reject(new Error('Cannot count to ' + error.message)),
);
});
});
11 changes: 6 additions & 5 deletions src/subscription/mapAsyncIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
export function mapAsyncIterator<T, U>(
iterable: AsyncIterable<T> | AsyncGenerator<T, void, void>,
callback: (T) => PromiseOrValue<U>,
rejectCallback: (any) => PromiseOrValue<U> = (error) => {
rejectCallback: (any) => U = (error) => {
throw error;
},
): AsyncGenerator<U, void, void> {
Expand All @@ -31,10 +31,11 @@ export function mapAsyncIterator<T, U>(
}

function mapReject(error: mixed) {
return asyncMapValue(error, rejectCallback).then(
iteratorResult,
abruptClose,
);
try {
return { value: rejectCallback(error), done: false };
} catch (callbackError) {
return abruptClose(callbackError);
}
}

/* TODO: Flow doesn't support symbols as keys:
Expand Down