svelte: Svelte 5: onMount accepts async function, $effect does not

Describe the bug

In most cases, I’m trying to replace onMount with $effect, which seems to be the better way to go. However, $effect doesn’t allow an async function:

<script lang="ts">
  import type { Snippet } from 'svelte'

  interface Props {
    theme: string
    children?: Snippet
  }

  let { theme, children }: Props = $props()

  $effect(async () => {
    await import(`@styles/themes/${theme}.scss`)
  })
</script>

{@render children?.()}

Tying Error:

 Argument of type '() => Promise<void>' is not assignable to parameter of type '() => void | (() => void)'.
  Type 'Promise<void>' is not assignable to type 'void | (() => void)'.ts(2345)

Reproduction

See above

Logs

No response

System Info

System:
    OS: macOS 15.0.1
    CPU: (12) arm64 Apple M3 Pro
    Memory: 76.77 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.1 - ~/.asdf/installs/nodejs/20.15.1/bin/node
    Yarn: 4.3.1 - ~/.asdf/installs/nodejs/20.15.1/bin/yarn
    npm: 10.8.2 - ~/.asdf/plugins/nodejs/shims/npm
    bun: 1.1.0 - ~/.bun/bin/bun
  Browsers:
    Chrome: 130.0.6723.70
    Safari: 18.0.1
  npmPackages:
    svelte: ^5.1.2 => 5.1.2

Severity

annoyance

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17 (11 by maintainers)

Most upvoted comments

Is not that $effect disallows async functions, is that its TypeScript has been made not to take it. If you ignore the TS error and write async code in your effect, it works.

To me, disallowing async code in effects feels like you’re forcing me into declaring an extra function just for nothing. Why can’t I have the more succinct syntax? This is my opinion on the matter.

Ok, so let’s consider this use case (please ignore that there are obviously other ways to it, like using .then/.catch, but it should demonstrate a valid use case people might want to accomplish). I can’t use onMount because I want it to be triggered both on mount and when tab state is changed. But using await on the fetch is not possible unless the function is async. And function can’t be async because effect doesn’t allow it.

<script type="ts">
  let container: HtmlDivElement | undefined = $state()
  let tab = $state('dashboard')

  $effect(() => {
    const result = await fetch("/?tab=${tab}")
    container?.innerHTML = result
  })
</script>

<ul>
  <li onclick={() => { tab = 'dashboard' }}>Dashboard</li>
  <li onclick={() => { tab = 'settings' }}>Settings</li>
</ul>

<div bind:this={container}>
  Loading...
</div>

onMount() and $effect() has two different usage…

Event if it’s possible, I don’t see any interest in a hacky code to simulate onMount with $effect… Just use onMount() directly…

One less import and its kind of annoying that its the last lifecycle function thats really left. I find it kind of jarring to have $effect next to onMount in the same file.

If I remember correctly, back in the day when the types for $effect allowed async functions, the documentation stated very clearly what was tracked: “and up to the first await.”, I think it said. I understood well.

Anyway, you guys decide on this one. I was happy with async, now I’m not as happy, but maybe it’s just me.

Although @KieranP seems to be after $effect() for the wrong reasons on this particular instance, I’m with him in that $effect() should accept async functions. I know, this was removed because it causes confusion, etc. etc. Still, I think it is an overstep from Svelte, and the confusing cases should just be documented. My humble opinion.

onMount() is only run when the component is mounted. $effect() is run when the component is mounted, AND when any of the reactive variable are changed.

I didn’t see that you use a prop on your effect, but since you’re not using the return value of import(), just remove the async/await :

  $effect(() => {
    import(`@styles/themes/${theme}.scss`)
  })

And if your really need the return value, use .then() (be carefull because code inside then() will not be reactive).