1 of 22

Layering: Fix the jobs infrastructure

Presenter: Daniel Ehrenberg

Author: Domenic Denicola

June 2019 TC39 meeting

2 of 22

What are Jobs?

  • When a Promise is resolved or rejected,�a callback runs
    • Later
      • After the current code finishes
        • In a "Job"
          • Sometimes called "microtask"

3 of 22

4 of 22

When do Jobs run?

  • In HTML and Node.js, all Promise jobs run before the rest of the "event loop"
    • Event loop responsible for asynchronous I/O, updates to the DOM, etc.

5 of 22

When do Jobs run?

  • Algorithm in HTML and Node:
    • Run the initial JS code
    • Run all the Promise jobs
    • Return to the event loop
      • Which may call back into JS

6 of 22

This is a huge simplification

7 of 22

Goal: Let job ordering be specified precisely, to ensure interoperability between implementations

8 of 22

Embedder-specific ordering of event loops

  • In HTML
  • In Node.js

9 of 22

JavaScript specifies an algorithm full of implementation-defined steps.

Ordering, priority remain vague.

No clean "host hooks" for other specifications to use.

10 of 22

HTML doesn't use this part of the JavaScript specification

It specifies its own, precise algorithms

11 of 22

Why does this matter?

12 of 22

Some proposals may touch the job queue

  • Currently: Promises
  • WeakRefs
  • Atomics.waitAsync
  • (more?)
  • Plus, the current mismatch is just confusing
    • Including for implementers

13 of 22

Let's let embedders cleanly specify their own event loop and how the job queue interacts with it

14 of 22

15 of 22

The general approach

  • Ask the embedder to define its event loop, including the job queue, following specified invariants
  • HostEnqueueJob ( queueName, job, arguments )
  • Provides invariants matching current specification
  • Matches current layering in HTML
  • PR includes a large note with self-contained usage

16 of 22

Cross-environment invariants

17 of 22

18 of 22

19 of 22

Alternative: One-off callbacks

20 of 22

Approach taken by WeakRefs, Atomics.waitAsync

HostCleanupFinalizationGroup expected to queue a call to CleanupFinalizationGroup. No use of Jobs.

We could do this with Promises similarly

21 of 22

These are all normatively equivalent to RunJobs;

Editorial alterantives allow more precise use by embedders

22 of 22

Thoughts?

Adopt this notation?