Skip to content

Instantly share code, notes, and snippets.

@noamr
Last active September 13, 2024 07:33
Show Gist options
  • Save noamr/3ffb223d1a560db3a9a066d7117332c8 to your computer and use it in GitHub Desktop.
Save noamr/3ffb223d1a560db3a9a066d7117332c8 to your computer and use it in GitHub Desktop.
How to get a promise that resolves after the next paint
async function after_next_paint() {
const p = document.createElement("p");
p.innerText = "1"
p.style.transform = "rotateX(90deg)";
p.style.pointerEvents = "none";
p.style.position = "fixed";
p.style.left = "0";
p.style.top = "0";
p.setAttribute("elementtiming", "foo");
document.body.append(p);
await new Promise(resolve => {
new PerformanceObserver((e, observer) => {
resolve();
observer.disconnect();
}).observe({type: "element"});;
});
p.remove();
}
after_next_paint().then(() => console.log("after next paint"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment