Skip to content

Commit 297afd0

Browse files
fix: generate correct code for each blocks with async body (#16923)
* fix: generate correct code for `each` blocks with async body * fix: else branch of `each` block * chore: add expected html
1 parent 7f8e60f commit 297afd0

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

.changeset/wicked-goats-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: generate correct code for `each` blocks with async body

packages/svelte/src/compiler/phases/3-transform/server/visitors/EachBlock.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export function EachBlock(node, context) {
3232
each.push(b.let(node.index, index));
3333
}
3434

35-
each.push(.../** @type {BlockStatement} */ (context.visit(node.body)).body);
35+
const new_body = /** @type {BlockStatement} */ (context.visit(node.body)).body;
36+
37+
each.push(...(node.body.metadata.has_await ? [create_async_block(b.block(new_body))] : new_body));
3638

3739
const for_loop = b.for(
3840
b.declaration('let', [
@@ -55,7 +57,7 @@ export function EachBlock(node, context) {
5557
b.if(
5658
b.binary('!==', b.member(array_id, 'length'), b.literal(0)),
5759
b.block([open, for_loop]),
58-
fallback
60+
node.fallback.metadata.has_await ? create_async_block(fallback) : fallback
5961
)
6062
);
6163
} else {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
mode: ['async']
5+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--[--><!--[--><!--[--><!---->each<!--]--><!--]--> <!--[--><!--[!--><!---->else<!--]--><!--]--><!--]-->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{#each { length: 1 }}
2+
{@const data = await Promise.resolve("each")}
3+
{data}
4+
{/each}
5+
6+
{#each { length: 0 }}
7+
should not see this
8+
{:else}
9+
{@const data = await Promise.resolve("else")}
10+
{data}
11+
{/each}

0 commit comments

Comments
 (0)