diff --git a/src/compiler/compile/render_dom/wrappers/AwaitBlock.ts b/src/compiler/compile/render_dom/wrappers/AwaitBlock.ts index cc186d1c02..a1cfa03d25 100644 --- a/src/compiler/compile/render_dom/wrappers/AwaitBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/AwaitBlock.ts @@ -232,14 +232,7 @@ export default class AwaitBlockWrapper extends Wrapper { const dependencies = this.node.expression.dynamic_dependencies(); - let update_child_context; - if (this.then.value && this.catch.value) { - update_child_context = b`#child_ctx[${this.then.value_index}] = #child_ctx[${this.catch.value_index}] = ${info}.resolved;`; - } else if (this.then.value) { - update_child_context = b`#child_ctx[${this.then.value_index}] = ${info}.resolved;`; - } else if (this.catch.value) { - update_child_context = b`#child_ctx[${this.catch.value_index}] = ${info}.resolved;`; - } + const update_child_context = b`@update_child_context(${info}, #ctx, #dirty)`; if (dependencies.length > 0) { const condition = x` @@ -256,9 +249,7 @@ export default class AwaitBlockWrapper extends Wrapper { if (${condition}) { } else { - const #child_ctx = #ctx.slice(); ${update_child_context} - ${info}.block.p(#child_ctx, #dirty); } `); } else { @@ -269,11 +260,7 @@ export default class AwaitBlockWrapper extends Wrapper { } else { if (this.pending.block.has_update_method) { block.chunks.update.push(b` - { - const #child_ctx = #ctx.slice(); - ${update_child_context} - ${info}.block.p(#child_ctx, #dirty); - } + ${update_child_context} `); } } diff --git a/src/runtime/internal/await_block.ts b/src/runtime/internal/await_block.ts index b93f216b32..f279221487 100644 --- a/src/runtime/internal/await_block.ts +++ b/src/runtime/internal/await_block.ts @@ -83,3 +83,17 @@ export function handle_promise(promise, info) { info.resolved = promise; } } + +export function update_child_context(info, ctx, dirty) { + const child_ctx = ctx.slice(); + const { resolved } = info; + + if (info.current === info.then) { + child_ctx[info.value] = resolved; + } + if (info.current === info.catch) { + child_ctx[info.error] = resolved; + } + + info.block.p(child_ctx, dirty); +} diff --git a/test/runtime/samples/await-with-update-catch-scope/_config.js b/test/runtime/samples/await-with-update-catch-scope/_config.js new file mode 100644 index 0000000000..33deab9ba6 --- /dev/null +++ b/test/runtime/samples/await-with-update-catch-scope/_config.js @@ -0,0 +1,51 @@ +export default { + props: { + thePromise: new Promise((_) => {}), + }, + + html: ` +
error: undefined
+ `, + + async test({ assert, component, target }) { + await (component.thePromise = Promise.resolve("abc")); + + assert.htmlEqual( + target.innerHTML, + ` +
+ error: undefined + After Resolve: undefined +
+ ` + ); + + component.error = 'external error occurred'; + + assert.htmlEqual( + target.innerHTML, + ` +
+ error: ${component.error} + After Resolve: ${component.error} +
+ ` + ); + + try { + await (component.thePromise = Promise.reject("failure")); + } catch (error) { + // ignore + } + + assert.htmlEqual( + target.innerHTML, + ` +
+ error: ${component.error} + Rejected: failure +
+ ` + ); + }, +}; diff --git a/test/runtime/samples/await-with-update-catch-scope/main.svelte b/test/runtime/samples/await-with-update-catch-scope/main.svelte new file mode 100644 index 0000000000..4d4eb53f0b --- /dev/null +++ b/test/runtime/samples/await-with-update-catch-scope/main.svelte @@ -0,0 +1,13 @@ + + +
+ error: {error} + {#await thePromise then _} + After Resolve: {error} + {:catch error} + Rejected: {error} + {/await} +