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

fix(async): abortable should not change original outputs #5609

Merged
merged 7 commits into from
Aug 5, 2024
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
4 changes: 3 additions & 1 deletion async/abortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function abortable<T>(p: Promise<T>, signal: AbortSignal): Promise<T>;
* assertEquals(items, []);
* ```
*/

export function abortable<T>(
p: AsyncIterable<T>,
signal: AbortSignal,
Expand Down Expand Up @@ -153,7 +154,8 @@ async function* abortableAsyncIterable<T>(
const { done, value } = await race;
if (done) {
signal.removeEventListener("abort", abort);
return;
const result = await it.return?.(value);
return result?.value;
}
yield value;
}
Expand Down
40 changes: 40 additions & 0 deletions async/abortable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,43 @@ Deno.test("abortable.AsyncIterable() calls return before throwing", async () =>
assertEquals(items, []);
clearTimeout(timeoutId!);
});

Deno.test("abortable.AsyncIterable() behaves just like original when not aborted", async () => {
async function* gen() {
yield 1;
yield await Promise.resolve(2);
yield 3;
return 4;
}
const normalIterator = gen();
const abortController = new AbortController();
const abortableIterator = abortable(gen(), abortController.signal);

assertEquals(await abortableIterator.next(), await normalIterator.next());
assertEquals(await abortableIterator.next(), await normalIterator.next());
assertEquals(await abortableIterator.next(), await normalIterator.next());
assertEquals(await abortableIterator.next(), await normalIterator.next());
assertEquals(await abortableIterator.next(), await normalIterator.next());
});

Deno.test("abortable.AsyncIterable() behaves just like original when return is called", async () => {
async function* gen() {
yield 1;
yield await Promise.resolve(2);
yield 3;
return 4;
}
const normalIterator = gen();
const abortController = new AbortController();
const abortableIterator = abortable(gen(), abortController.signal);

assertEquals(
await abortableIterator.next(123),
await normalIterator.next(123),
);
assertEquals(
await abortableIterator.return(321),
await normalIterator.return(321),
Comment on lines +204 to +205
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still small difference in types of return function argument between normalIterator and abortableIterator.
But this can't be fixed without changing AsyncIterable<T> type.
denoland/deno#24837

);
assertEquals(await abortableIterator.next(), await normalIterator.next());
});