Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapAsyncIterator-test: check that return value is passed on early return #3066

Merged
merged 1 commit into from May 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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