-
Notifications
You must be signed in to change notification settings - Fork 35
✨ add interval() helper to consume a stream of intervals #1005
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
Conversation
commit: |
taras
left a comment
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.
Do you know if this change remains compatible with https://docs.deno.com/examples/mocking_tutorial/#faking-time?
|
@taras It definitely should because the interval helper is just another caller to We could actually use it to write a test to find out! .... but we'd have to make sure that it was compatible with nodejs |
|
🤦 I'm sorry, I read this in passing. I didn't notice that this is a contrib update - it doesn't change the underlying sleep command. |
taras
left a comment
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.
👍
jbolda
left a comment
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.
Seems great with me!
645caac to
63449eb
Compare
[`setInterval()`][1] is a core JavaScript API that allows you to setup
global state. As such, there really isn't any reason that you
shouldn't consume it using structured concurrency.
This adds an `interval()` function that constructs a stream that emits
an item every time that the interval is triggered.
```ts
let startTime = Date.now();
for (let _ of yield* each(interval(10))) {
let elapsed = Date.now() - startTime;
console.log(`elapsed time: ${elapsed} ms`);
}
```
[1]: https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval
63449eb to
1bbc61d
Compare
✨ add interval() helper to consume a stream of intervals
Motivation
setInterval()is a core JavaScript API that allows you to setup global state. As such, there really isn't any reason that you shouldn't consume it using structured concurrency.Approach
This adds an
interval()function that constructs a stream that emits an item every time that the interval is triggered.