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: add timeout function #250

Conversation

MarlonPassos-git
Copy link
Contributor

@MarlonPassos-git MarlonPassos-git commented Sep 18, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

This PR introduces a timeout promise function similar to the delay function, but with the ability to reject upon timeout. It also supports custom error classes passing a function as a parameter.

// Rejects after 1 second with the default "timeout" error message
await _.timeout(1000)

// Rejects after 1 second with a custom error message
await _.timeout(1000, 'Custom timeout message')

// Rejects after 1 second with a custom error object
await _.timeout(1000, () => new Error('Custom error'))

Related issue, if any:

#203

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed

Does this PR introduce a breaking change?

No

Bundle impact

Status File Size 1
A src/async/TimeoutError.ts 127
A src/async/timeout.ts 246

Footnotes

  1. Function size includes the import dependencies of the function.

@MarlonPassos-git MarlonPassos-git changed the title feat: add timeout feat: add timeout function Sep 18, 2024
@MarlonPassos-git MarlonPassos-git added the new feature This PR adds a new function or extends an existing one label Sep 18, 2024
@aleclarson aleclarson added this to the v12.3.0 milestone Sep 21, 2024
Copy link
Contributor

Choose a reason for hiding this comment

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

The timeout.test.ts is misspelled as timout.test.ts 🙃

@aleclarson
Copy link
Member

Hey! There's a new requirement for PRs that introduce new features. Without this requirement met, we won't be able to merge this. Note that this PR can still be included in a beta prerelease before this requirement is fulfilled.

⚠️ Note: You need to run git rebase main before this file will appear locally.

@MarlonPassos-git
Copy link
Contributor Author

@aleclarson about the next-minor.md file, you can check if this is the expected pattern. Maybe we can add some examples in contributing.md to make it easier for new contributors

@aleclarson
Copy link
Member

aleclarson commented Nov 11, 2024

Maybe we can add some examples in contributing.md to make it easier for new contributors

Great idea 👍

This was the expected format:

#### timeout

https://github.com/radashi-org/radashi/pull/250

But using #250 is also acceptable.

The idea is that a script will parse that out and fetch the documentation for each function. The parsing could be relatively loose, so an exact pattern is not strictly required.

If a PR is extending an existing function, I don't know if fetching the related docs will work (or at least, it won't be as easy), so the PR author might be on the hook for describing the new feature in next-minor.md etc.

@aleclarson aleclarson added the prerelease Publish to NPM under the "beta" or "next" tag label Nov 11, 2024
radashi-bot pushed a commit that referenced this pull request Nov 11, 2024
@radashi-bot
Copy link

Benchmark Results

Name Current
timeout: with default error message 921.57 ops/sec ±0.41%
timeout: with custom error message 920.74 ops/sec ±0.42%
timeout: with custom error function 925.63 ops/sec ±0.74%

Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure.

@aleclarson
Copy link
Member

We don't benchmark async functions (or at least, we haven't been).

@aleclarson aleclarson force-pushed the feature/add-timeout-promise-function branch from 444fe2b to d6cff01 Compare December 1, 2024 15:44
@aleclarson aleclarson merged commit 6a0ba7e into radashi-org:main Dec 1, 2024
9 checks passed
@aleclarson
Copy link
Member

Thanks for everything, Marlon! 🚀

@radashi-bot
Copy link

A stable release 12.3.0 has been published to NPM. 🚀

To install:

See the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This PR adds a new function or extends an existing one prerelease Publish to NPM under the "beta" or "next" tag
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants