Skip to content

Commit 6c53a27

Browse files
mapAsyncIterator: allow 'rejectCallback' to only be synchronous (#3012)
1 parent 0f3e91f commit 6c53a27

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/subscription/__tests__/mapAsyncIterator-test.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,19 @@ describe('mapAsyncIterator', () => {
354354
);
355355
});
356356

357-
async function testClosesSourceWithRejectMapper<T>(mapper: (Error) => T) {
357+
it('closes source if mapper throws an error', async () => {
358358
async function* source() {
359359
yield 1;
360360
throw new Error(2);
361361
}
362362

363-
const throwOver1 = mapAsyncIterator(source(), (x) => x, mapper);
363+
const throwOver1 = mapAsyncIterator(
364+
source(),
365+
(x) => x,
366+
(error) => {
367+
throw new Error('Cannot count to ' + error.message);
368+
},
369+
);
364370

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

@@ -379,17 +385,5 @@ describe('mapAsyncIterator', () => {
379385
value: undefined,
380386
done: true,
381387
});
382-
}
383-
384-
it('closes source if mapper throws an error', async () => {
385-
await testClosesSourceWithRejectMapper((error) => {
386-
throw new Error('Cannot count to ' + error.message);
387-
});
388-
});
389-
390-
it('closes source if mapper rejects', async () => {
391-
await testClosesSourceWithRejectMapper((error) =>
392-
Promise.reject(new Error('Cannot count to ' + error.message)),
393-
);
394388
});
395389
});

src/subscription/mapAsyncIterator.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
77
export function mapAsyncIterator<T, U>(
88
iterable: AsyncIterable<T> | AsyncGenerator<T, void, void>,
99
callback: (T) => PromiseOrValue<U>,
10-
rejectCallback: (any) => PromiseOrValue<U> = (error) => {
10+
rejectCallback: (any) => U = (error) => {
1111
throw error;
1212
},
1313
): AsyncGenerator<U, void, void> {
@@ -31,10 +31,11 @@ export function mapAsyncIterator<T, U>(
3131
}
3232

3333
function mapReject(error: mixed) {
34-
return asyncMapValue(error, rejectCallback).then(
35-
iteratorResult,
36-
abruptClose,
37-
);
34+
try {
35+
return { value: rejectCallback(error), done: false };
36+
} catch (callbackError) {
37+
return abruptClose(callbackError);
38+
}
3839
}
3940

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

0 commit comments

Comments
 (0)