diff --git a/source b/source index b3f8e1b1f56..0a8e3bcdc09 100644 --- a/source +++ b/source @@ -3930,6 +3930,17 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute +
Cooperative Scheduling of Background Tasks
+ +
+

The following feature is defined in the Cooperative Scheduling of Background + Tasks specification:

+ + +
+
@@ -88815,7 +88826,7 @@ dictionary PromiseRejectionEventInit : EventInit {

To affect the world of observable JavaScript objects, then, you must queue a task to perform any such manipulations. This ensures your steps are properly interleaved with respect to other things happening on the event loop. Furthermore, you must choose a - task source when queueing a task; this governs the + task source when queuing a task; this governs the relative order of your steps versus others. If you are unsure which task source to use, pick one of the generic task sources that sounds most applicable.

@@ -88825,7 +88836,7 @@ dictionary PromiseRejectionEventInit : EventInit { algorithms to be invoked in contexts involving multiple event loops. (Unlike contexts involving multiple global objects, which happen all the time!) So unless you are writing a specification which, e.g., deals with manipulating workers, you can omit this argument - when queueing a task.

+ when queuing a task.

Putting this all together, we can provide a template for a typical algorithm that needs to do work asynchronously:

@@ -89816,6 +89827,9 @@ interface WindowOrWorkerGlobalScope { long setInterval(TimerHandler handler, optional long timeout = 0, any... arguments); void clearInterval(optional long handle = 0); + // microtask queuing + void queueMicrotask(Function callback); + // ImageBitmap Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, optional ImageBitmapOptions options); Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, long sx, long sy, long sw, long sh, optional ImageBitmapOptions options); @@ -90901,6 +90915,35 @@ scheduleWork(); // queues a task to do lots of work +

Microtask queuing

+ +

The queueMicrotask() method allows authors to schedule + a callback on the microtask queue. This allows their code to run after the + currently-executing task has run to completion and the + JavaScript execution context stack is empty, but without yielding control back to the + browser's event loop, as would be the case when using, for example, setTimeout(f, 0).

+ +

Authors should be aware that scheduling a lot of microtasks has the same performance downsides + as running a lot of synchronous code. Both will prevent the browser from doing its own work, such + as rendering or scrolling. In many cases, requestAnimationFrame() or + requestIdleCallback() is a better choice.

+ +
+
self . queueMicrotask(callback)
+

Queues a microtask to run the given + callback.

+
+ +

The queueMicrotask(callback) method must + queue a microtask to invoke + callback, and if callback throws an exception, report the + exception.

+ +

User prompts