diff --git a/.changeset/wise-bottles-explode.md b/.changeset/wise-bottles-explode.md new file mode 100644 index 0000000000..d3b2931272 --- /dev/null +++ b/.changeset/wise-bottles-explode.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure tick resolves within a macrotask diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 22a1890e0f..b8f5f5ffc9 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -500,7 +500,13 @@ export function update_effect(effect) { */ export async function tick() { if (async_mode_flag) { - return new Promise((f) => requestAnimationFrame(() => f())); + return new Promise((f) => { + // Race them against each other - in almost all cases requestAnimationFrame will fire first, + // but e.g. in case the window is not focused or a view transition happens, requestAnimationFrame + // will be delayed and setTimeout helps us resolve fast enough in that case + requestAnimationFrame(() => f()); + setTimeout(() => f()); + }); } await Promise.resolve();