-
Notifications
You must be signed in to change notification settings - Fork 3
Add JS Async implementation and make it the default #2
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
base: master
Are you sure you want to change the base?
Conversation
This package only provides a synchronous version of each function and especially for the `slowAfter` this might be a problem because it uses a while loop for sleeping. This Commit renames the existing functions to their *Sync version and adds async implementations (which most of the time just use the sync one). It also makes the async ones the default, so a new developer would be encouraged to use those functions instead. Tests are added to reflect the sync versions and also readme is updated. Signed-off-by: Raphael Höser <[email protected]>
Signed-off-by: Raphael Höser <[email protected]>
| } | ||
|
|
||
| export function warnAfter(time: Date, options: Partial<Config> = {}) { | ||
| export async function warnAfter(time: Date, options: Partial<Config> = {}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer not to clutter up the lib with async versions of things that shouldn't take time -- given that await undefined === await Promise.resolve(undefined), i'm not sure if there's a use case for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intention here was to have all async versions return a promise to make this lib easier to use.
For the same reason I included a sync and async version for everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I get the intention, but I think waitAfter (non-sync) would be the only use case I can imagine for using the return value, so if it comes down to a choice between removing a bunch of duplicate functions where it's unclear why they exist vs returning an instantly resolving promise, I'd choose the former.
Or I'd just modify the originals to return Promise.resolve(undefined) and remove the sync versions entirely, but even that I feel is unnecessary.
Thanks for your input!
| } | ||
|
|
||
| export function slowAfter( | ||
| export async function slowAfter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NB: I'd prefer if we managed to figure out a way to share the logic between slowAfter and slowAfterSync-- maybe a generic higher-order-function that returns the result of wait?
function buildSlowAfter<T>(waitFn: ((number) => A)){
return (time, delay, options) => {
...
}
}
const slowAfter = buildSlowAfter(wait);
const slowAfterSync = buildSlowAfter(waitSync);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this lib (like right now) doesn't need to do anything after the wait, we could wrap the loop in a promise (since the executer gets executed in the same callstack if I'm not mistaken).
This would make a higher order function easier to implement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! Do whatever you think is cleanest -- i just proposed what I thought might work cleanly.
|
Thank you so much for your contribution! Just a few comments :) |
This package only provides a synchronous version of each function and especially for the
slowAfterthis might be a problem because it uses a while loop for sleeping.This Commit renames the existing functions to their *Sync version and adds async implementations (which most of the time just use the sync one).
It also makes the async ones the default, so a new developer would be encouraged to use those functions instead.
Tests are added to reflect the sync versions and also readme is updated.