Order of callbacks: setTimeout and ResizeObserver

76 Views Asked by At

With the following code I sometimes get timeout, then RO output, sometimes the reverse. Are both valid browser behaviours? Or just one?

const div = document.createElement("div")
document.body.append(div)
new ResizeObserver(() => console.log("RO")).observe(div)
setTimeout(() => console.log("timeout"))

It happens in Firefox. In Midori, which also uses Gecko, it seems to be always timeout first.

EDIT: Simpler code also showing this:

new ResizeObserver(() => console.log("RO")).observe(document.body)
setTimeout(() => console.log("timeout"))

EDIT: Jake Archibald's article and other materials indicate, IIUC, that the observers should run first. That is, assuming that ResizeObserver queues a microtask, like MutationObserver does. Alas, the Editor's Draft for ResizeObserver seems silent on that.

0

There are 0 best solutions below