Skip to content

Commit 5579180

Browse files
mapAsyncIterator-test: check that return value is passed on early return (#3066)
1 parent 960f207 commit 5579180

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/subscription/__tests__/mapAsyncIterator-test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,16 @@ describe('mapAsyncIterator', () => {
9090

9191
it('allows returning early from mapped async generator', async () => {
9292
async function* source() {
93-
yield 1;
94-
yield 2;
93+
try {
94+
yield 1;
95+
yield 2;
9596

96-
// istanbul ignore next (Shouldn't be reached)
97-
yield 3;
97+
// istanbul ignore next (Shouldn't be reached)
98+
yield 3;
99+
} finally {
100+
// eslint-disable-next-line no-unsafe-finally
101+
return 'The End';
102+
}
98103
}
99104

100105
const doubles = mapAsyncIterator(source(), (x) => x + x);
@@ -104,7 +109,7 @@ describe('mapAsyncIterator', () => {
104109

105110
// Early return
106111
expect(await doubles.return()).to.deep.equal({
107-
value: undefined,
112+
value: 'The End',
108113
done: true,
109114
});
110115

src/subscription/mapAsyncIterator.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
44
* Given an AsyncIterable and a callback function, return an AsyncIterator
55
* which produces values mapped via calling the callback function.
66
*/
7-
export function mapAsyncIterator<T, U>(
8-
iterable: AsyncIterable<T> | AsyncGenerator<T, void, void>,
7+
export function mapAsyncIterator<T, U, R = void>(
8+
iterable: AsyncGenerator<T, R, void> | AsyncIterable<T>,
99
callback: (T) => PromiseOrValue<U>,
10-
): AsyncGenerator<U, void, void> {
10+
): AsyncGenerator<U, R, void> {
1111
// $FlowIssue[incompatible-use]
1212
const iterator = iterable[Symbol.asyncIterator]();
1313

1414
async function mapResult(
15-
result: IteratorResult<T, void>,
16-
): Promise<IteratorResult<U, void>> {
15+
result: IteratorResult<T, R>,
16+
): Promise<IteratorResult<U, R>> {
1717
if (result.done) {
1818
return result;
1919
}
@@ -37,7 +37,7 @@ export function mapAsyncIterator<T, U>(
3737
async next() {
3838
return mapResult(await iterator.next());
3939
},
40-
async return(): Promise<IteratorResult<U, void>> {
40+
async return(): Promise<IteratorResult<U, R>> {
4141
return typeof iterator.return === 'function'
4242
? mapResult(await iterator.return())
4343
: { value: undefined, done: true };

0 commit comments

Comments
 (0)