-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Description
Describe the bug
In the documentation for the mount function, there's this paragraph:
Note that unlike calling new App(...) in Svelte 4, things like effects (including onMount callbacks, and action functions) will not run during mount. If you need to force pending effects to run (in the context of a test, for example) you can do so with flushSync().
Therefore you would expect to need to call flushSync()
if you want the onMount
callback to run for example, but I found a (simpler) solution from another issue (#15105 (comment)) which consisted of using await mount(...)
instead of mount(...)
. I don't know if this is intended or not, but unless I'm mistaken, this isn't mentioned anywhere in the Svelte documentation, nor in the function's signature.
Perhaps it would be better to mention this somewhere in the documentation, and optionally to make the function return a promise (or create an async mount function that returns a promise) because the svelte language server is confused as to why I use await
on a synchronous function.
Reproduction
I've created a minimal example here. You can see (in the console) that when mounting synchronously, the call to mount
finishes before the onMount
handler executes, and asynchronously, the onMount
handler executes before the call to mount
finishes because of the await
.
Logs
System Info
System:
OS: Linux 6.8 Linux Mint 22.1 (Xia)
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Memory: 5.70 GB / 15.50 GB
Container: Yes
Shell: 5.9 - /usr/bin/zsh
Binaries:
Node: 22.17.1 - ~/.nvm/versions/node/v22.17.1/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v22.17.1/bin/yarn
npm: 10.9.2 - ~/.nvm/versions/node/v22.17.1/bin/npm
pnpm: 10.14.0 - ~/.local/share/pnpm/pnpm
bun: 1.2.16 - /usr/local/bin/bun
Browsers:
Chrome: 131.0.6778.204
npmPackages:
svelte: ^5.0.0 => 5.38.0
Severity
annoyance