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

Promise returning version of setTimeout #3875

Closed
benjamingr opened this issue Aug 2, 2018 · 3 comments
Closed

Promise returning version of setTimeout #3875

benjamingr opened this issue Aug 2, 2018 · 3 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest

Comments

@benjamingr
Copy link
Member

benjamingr commented Aug 2, 2018

With the popularity of async/await in JavaScript and the adoption of DOM futures (promises) across new DOM APIs one pretty common request I've ran into is a promise returning version of setTimeout.

This can either be achieved by overloading setTimeout or exposing a new method that returns a promise:

await delay(3000); // similar to `new Promise(r => setTimeout(4, 3000))`

Ideally, it wouldn't have the string-as-parameter behaviour. One argument against this is that while it's common it's also pretty easily implemented (with the promise constructor). This is true - though I've seen people make mistakes implementing it and it's so common it might make sense to ship it from the platform.

Node also added a util.promisify.custom on setTimeout so it's easy to get a version of setTimeout that does this in Node.

I'm not really decided myself - I'm mostly asking for this because of API discussions on a new platform that needs timers and the fact I'd rather go with a specified standard than without one - it seems weird to add setTimeout as specified in the timers spec taking a callback.

@wanderview
Copy link
Member

I'm somewhat ambivalent, but any such API probably also needs something equivalent to clearTimeout(). Maybe using AbortSignal/AbortController.

@annevk annevk added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Aug 2, 2018
@benjamingr
Copy link
Member Author

I'm somewhat ambivalent, but any such API probably also needs something equivalent to clearTimeout(). Maybe using AbortSignal/AbortController.

Sure, what if we allow passing an AbortController.signal as an optional second parameter?

await delay(3000, { signal: controller.signal} );

In a way similar to fetch?

Another interesting idea is a variant of setInterval that returns an async iterator, though we might not want to tie the two together.

for await(const time of interval(1000)) {
 // do something, `break` here to cancel (or `.return` on the iterator)
}

@domenic
Copy link
Member

domenic commented Aug 2, 2018

Let's dupe this into #617, which already has some discussion on cancelation.

@domenic domenic closed this as completed Aug 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest
Development

No branches or pull requests

4 participants