-
Notifications
You must be signed in to change notification settings - Fork 0
Implement promise-based API for setRecursive with AsyncIterator #20
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
Co-authored-by: parzhitsky <59528510+parzhitsky@users.noreply.github.com>
Co-authored-by: parzhitsky <59528510+parzhitsky@users.noreply.github.com>
…examples Co-authored-by: parzhitsky <59528510+parzhitsky@users.noreply.github.com>
…and remove unused types Co-authored-by: parzhitsky <59528510+parzhitsky@users.noreply.github.com>
parzhitsky
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.
This is no different from the built-in setInterval:
import { setInterval } from 'timers/promises'
for await (const _ of setInterval(1000)) {
console.log('tick')
}
The value of setRecursive should be that it doesn't just blindly waits for whatever there delay is, but also waits for the handler to finish before starting the new countdown.
This already works with the callback-based interface (where the handler is provided as a callback argument). Now define a promise-based version with whatever interface makes most sense (developing an interface is part of the task).
…delay after work Co-authored-by: parzhitsky <59528510+parzhitsky@users.noreply.github.com>
Fixed in commit 6abe8f9. The promise-based API now correctly matches the callback-based behavior:
This ensures the delay is truly BETWEEN iterations, just like the callback version where the callback executes first, then the next timeout is scheduled. Timeline comparison:
The implementation tracks whether it's the first iteration and skips the delay only for that first call, ensuring subsequent iterations wait for the full delay after the previous iteration's work completes. |
Adds a promise-based interface for
setRecursivemodeled after Node.jstimers/promises, enablingfor await...ofusage with critical enhancements that match the callback-based behavior: first iteration is immediate and delays happen after async work completes before scheduling the next iteration.Implementation
RecursiveTimeout<T>that yields optional valuessignaloption, throwing properDOMExceptionwithAbortErrornamependingRejectcallback and event listener removalAPI
Key Differences from Node.js
Unlike Node.js
setIntervalwhich delays before each iteration,setRecursive:This matches the callback-based
setRecursivebehavior where the callback executes first, then the next timeout is scheduled.Testing
Added 11 tests covering:
clearRecursive()next(),return())Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.