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

feat(async): support signal on deadline() like delay() #3347

Merged
merged 3 commits into from
May 2, 2023

Conversation

lambdalisue
Copy link
Contributor

@lambdalisue lambdalisue commented Apr 30, 2023

As the subject suggests, I have added support for the signal parameter in the deadline() function. This change is useful to clean up dangling promises for tests like:

Deno.test("...", async () => {
  const controller = new AbortController();
  const { signal } = controller;
  assertEquals(await Promise.race([
    deadline(new Promise(() => {}), 100, { signal }),
    Promise.resolve("Hello"),
  ], "Hello");
  controller.abort();
});

Without the signal parameter, the above test fails with the following error:

error: Leaking async ops:
  - 1 async operation to sleep for a duration was started in this test, but never completed. This is often caus
ed by not cancelling a `setTimeout` or `setInterval` call.
To get more details where ops were leaked, run again with --trace-ops flag.

Note that the comment on deadline suggests using AbortSignal.timeout(...) instead. However, the signal itself cannot be manually aborted (yet), so deadline() is still useful in such cases.

@lambdalisue lambdalisue requested a review from kt3k as a code owner April 30, 2023 13:55
Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@kt3k kt3k merged commit 264c714 into denoland:main May 2, 2023
@lambdalisue lambdalisue deleted the deadline-abort branch May 2, 2023 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants