Sub topic 11
The ViewTransition object: ready, finished, updateCallbackDone
This chapter assumes you can use promises and async/await. It also assumes you know the lifecycle from earlier chapters: capture the old state, run the update callback, capture the new state, then animate.
What startViewTransition() returns
document.startViewTransition() returns a ViewTransition object. It has three promises, one for each stage of the lifecycle, and a skipTransition() method.
| Promise | Fulfills when | Rejects when |
|---|---|---|
| `updateCallbackDone` | The promise returned by your update callback fulfills | The callback throws or its promise rejects |
| `ready` | The pseudo-elements exist and the animation is about to start | The transition is skipped or cannot start, or the callback fails |
| `finished` | The animation has ended and the pseudo-elements are removed | The callback fails |
Two rules explain most surprises:
updateCallbackDonetells you about the DOM, not the animation. It still fulfills when the transition is skipped.finishedfulfills even when the animation is skipped, as long as the update itself succeeded. If you need to know whether the animation actually ran, checkready.
Using each promise
Use updateCallbackDone when code depends on the new DOM, such as moving focus. Use ready to start JavaScript animations on the pseudo-elements. Use finished for cleanup.
async function openPanel(panel) {
const transition = document.startViewTransition(() => {
panel.hidden = false;
});
// The DOM is updated, even if the animation never runs.
await transition.updateCallbackDone;
panel.querySelector('button, [href], input')?.focus();
try {
await transition.ready;
// Pseudo-elements exist here. Start any JS animation now.
} catch {
// Skipped or failed to start. The page still works.
}
await transition.finished;
panel.classList.remove('is-animating');
}Mark panel with is-animating before the call if your CSS needs it during the transition. The cleanup runs after finished, so the class stays for the whole animation.
Handling rejections
An unhandled rejection from ready shows up in the console whenever a transition is skipped. This happens, for example, when a second startViewTransition() call interrupts the first. Attach a handler if you do not use the promise:
const transition = document.startViewTransition(() => updateView());
transition.ready.catch(() => {});The update callback can also return a promise, for example after fetching data. The browser keeps showing the old snapshot until that promise settles, so slow work in the callback freezes the page visually.
Calling skipTransition()
transition.skipTransition() ends the animation early. The update callback still runs, so the DOM always reaches the new state. ready rejects if the animation had not started yet, and finished still fulfills.
Exercise
Your update callback fetches data and rejects because the network is down. Which of updateCallbackDone, ready, and finished reject?
Show answer
All three reject. The callback failed, so the update did not complete. updateCallbackDone and finished report the callback's failure, and ready rejects because the transition cannot start without a new state to animate to.