Skip to content

await isn't allowed in non-async function when assigning {@const ... await} inside {#each...} block #16922

@Tinggaard

Description

@Tinggaard

Describe the bug

It is not possible to use asynchronous function calls ({@const foo = await fn()}) inside of an {#each} block.

It is possible to mitigate it by passing the data directly to a component instead.

Reproduction

{#each { length: 1 } as _}
  {@const data = await Promise.resolve("data")}
{/each}

https://stackblitz.com/github/tinggaard/await-in-non-async-fn

Edit: For some reason this does not throw in the Svelte REPL. see:
https://svelte.dev/playground/68548d8982b34b35a121594bda946ad5

Logs

[vite] (ssr) Error when evaluating SSR module /src/routes/+page.svelte: Parse failure: await isn't allowed in non-async function
At file: /src/routes/+page.svelte:16:18
  File: /src/routes/+page.svelte:16:18
  14 |  			for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
  15 |  				let _ = each_array[$$index];
  16 |  				const data = (await $.save(Promise.resolve("data")))();
     |                    ^
  17 |  			}
  18 |

System Info

System:
    OS: Linux 6.17 Arch Linux
    CPU: (16) x64 AMD Ryzen 7 8845HS w/ Radeon 780M Graphics
    Memory: 7.12 GB / 27.15 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.9.0 - /usr/bin/node
    npm: 11.6.1 - /usr/bin/npm
    pnpm: 10.18.0 - /usr/bin/pnpm
  Browsers:
    Brave Browser: 139.1.81.137
    Chromium: 141.0.7390.54
    Firefox: 143.0.4
    Firefox Developer Edition: 143.0.4
  npmPackages:
    @sveltejs/adapter-auto: ^6.1.1 => 6.1.1 
    @sveltejs/kit: ^2.46.4 => 2.46.4 
    @sveltejs/vite-plugin-svelte: ^6.2.1 => 6.2.1 
    svelte: ^5.39.11 => 5.39.11 
    vite: ^7.1.9 => 7.1.9

Severity

blocking an upgrade

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions