# Pastebin kYO310cz let gCallbacks = { beforeLayout: [], afterLayout: [], }; function runCallbacksForThisFrame() { for (const callback of gCallbacks.beforeLayout) { callback(); } document.body.offsetTop; // Trigger layout for (const callback of gCallbacks.afterLayout) { callback(); } } function promiseLayoutFlushed(callback) { // Assume all the promise stuff... if (gCallbacks.beforeLayout.length === 0 && gCallbacks.afterLayout.length === 0) { requestAnimationFrame(runCallbacksForThisFrame); } gCallbacksForThisFrame.afterLayout.push(callback); } function promiseLayoutFlushed(callback) { if (gCallbacks.beforeLayout.length === 0 && gCallbacks.afterLayout.length === 0) { requestAnimationFrame(runCallbacksForThisFrame); } gCallbacksForThisFrame.beforeLayout.push(callback); }