Skip to content

Commit

Permalink
mapAsyncIterator-test: check that return value is passed on early ret…
Browse files Browse the repository at this point in the history
…urn (#3066)
  • Loading branch information
IvanGoncharov committed May 8, 2021
1 parent 960f207 commit 5579180
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/subscription/__tests__/mapAsyncIterator-test.js
Expand Up @@ -90,11 +90,16 @@ describe('mapAsyncIterator', () => {

it('allows returning early from mapped async generator', async () => {
async function* source() {
yield 1;
yield 2;
try {
yield 1;
yield 2;

// istanbul ignore next (Shouldn't be reached)
yield 3;
// istanbul ignore next (Shouldn't be reached)
yield 3;
} finally {
// eslint-disable-next-line no-unsafe-finally
return 'The End';
}
}

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

// Early return
expect(await doubles.return()).to.deep.equal({
value: undefined,
value: 'The End',
done: true,
});

Expand Down
12 changes: 6 additions & 6 deletions src/subscription/mapAsyncIterator.js
Expand Up @@ -4,16 +4,16 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
* Given an AsyncIterable and a callback function, return an AsyncIterator
* which produces values mapped via calling the callback function.
*/
export function mapAsyncIterator<T, U>(
iterable: AsyncIterable<T> | AsyncGenerator<T, void, void>,
export function mapAsyncIterator<T, U, R = void>(
iterable: AsyncGenerator<T, R, void> | AsyncIterable<T>,
callback: (T) => PromiseOrValue<U>,
): AsyncGenerator<U, void, void> {
): AsyncGenerator<U, R, void> {
// $FlowIssue[incompatible-use]
const iterator = iterable[Symbol.asyncIterator]();

async function mapResult(
result: IteratorResult<T, void>,
): Promise<IteratorResult<U, void>> {
result: IteratorResult<T, R>,
): Promise<IteratorResult<U, R>> {
if (result.done) {
return result;
}
Expand All @@ -37,7 +37,7 @@ export function mapAsyncIterator<T, U>(
async next() {
return mapResult(await iterator.next());
},
async return(): Promise<IteratorResult<U, void>> {
async return(): Promise<IteratorResult<U, R>> {
return typeof iterator.return === 'function'
? mapResult(await iterator.return())
: { value: undefined, done: true };
Expand Down

0 comments on commit 5579180

Please sign in to comment.