whatwg / html

HTML Standard
https://html.spec.whatwg.org/multipage/
Other
8.1k stars 2.66k forks source link

Awaitable timers #7340

Closed jasnell closed 2 years ago

jasnell commented 2 years ago

In Node.js 15.x, we introduced the concept of awaitable timers... variants of setTimeout() and setInterval() that return promises. For instance:

https://nodejs.org/dist/latest-v17.x/docs/api/timers.html#timers-promises-api

import { setTimeout } from 'timers/promises';

// Halts execution for approximately 1 second without blocking the event loop
await setTimeout(1000);

https://nodejs.org/dist/latest-v17.x/docs/api/timers.html#timerspromisessetintervaldelay-value-options

import { setInterval } from 'timers/promises';

// Iterates approximately once per second without blocking the event loop
for await (const _ of setInterval(1000) {
  console.log('.');
}

These have proven to be very useful for a number of cases and I believe it's worthwhile to at least discuss whether they should be standardized in some variation within the HTML spec.

annevk commented 2 years ago

See https://wicg.github.io/scheduling-apis/#sec-scheduler. The idea is that scheduler.postTask() is that API. The other thing we want to do is introduce AbortSignal.timeout(): https://github.com/whatwg/dom/issues/951.

domenic commented 2 years ago

Let's dupe into https://github.com/whatwg/html/issues/617 and continue the discussion there.