In what order do the yield continuation and the timeout run?
The tea list's reviews.js runs as one task, and numbers every line it logs. It logs whether the browser has scheduler.yield(), then queues three tasks that each log a line: a 0 ms setTimeout, a scheduler.postTask() with "background" priority, and one with "user-blocking" priority, in that order. Then it calls readReviews(), which logs a line, awaits scheduler.yield(), and logs another, and finally it logs that the script has reached its end. In what order are the lines logged in Chrome?
index.html<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Leaf and Kettle</title>
</head>
<body>
<h1>Leaf and Kettle</h1>
<p id="reviews">Reading this week's reviews</p>
<script src="reviews.js"></script>
</body>
</html>
reviews.js// Each line is numbered, in the order it's logged.
let step = 0;
const log = (line) => console.log(++step + ". " + line);
log("scheduler.yield exists: " + (typeof globalThis.scheduler?.yield === "function"));
// Three tasks queued before the yield: a timer, then a background task, then a user-blocking one.
setTimeout(() => log("the timeout"), 0);
scheduler.postTask(
() => {
log("the background task");
document.body.dataset.logged = "true";
},
{ priority: "background" },
);
scheduler.postTask(() => log("the user-blocking task"), { priority: "user-blocking" });
async function readReviews() {
log("readReviews, before the yield");
await scheduler.yield();
log("readReviews, after the yield");
}
readReviews();
log("the end of reviews.js");
Chrome loads these files from a server on the same computer, index.html at /, in a fresh profile with an empty cache, on a phone's screen (412 by 823 CSS pixels), with no network or CPU slowdown.
- You open the page in Chrome and don't touch it.
- The script logs a line from each task as it runs.
Practice it in the editor, with tests
The answer
Work out what Chrome does with the page: every line it logs, in order, and how it treats each file. Then check.
Checking your sign-in…