Skip to content

Commit e0e4342

Browse files
authored
Await: re-throw error when there is no catch block and promise is rejected (#5149)
1 parent cd95654 commit e0e4342

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/compiler/compile/render_dom/wrappers/AwaitBlock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export default class AwaitBlockWrapper extends Wrapper {
188188
ctx: #ctx,
189189
current: null,
190190
token: null,
191+
hasCatch: ${this.catch.node.start !== null ? 'true' : 'false'},
191192
pending: ${this.pending.block.name},
192193
then: ${this.then.block.name},
193194
catch: ${this.catch.block.name},

src/runtime/internal/await_block.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export function handle_promise(promise, info) {
5959
update(info.then, 1, info.value, value);
6060
set_current_component(null);
6161
}, error => {
62+
if (!info.hasCatch) {
63+
throw error;
64+
}
6265
set_current_component(current_component);
6366
update(info.catch, 2, info.error, error);
6467
set_current_component(null);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
let fulfil;
2+
3+
let promise = new Promise(f => {
4+
fulfil = f;
5+
});
6+
7+
export default {
8+
props: {
9+
promise
10+
},
11+
12+
html: `
13+
<p>loading...</p>
14+
`,
15+
16+
test({ assert, component, target }) {
17+
fulfil(42);
18+
19+
return promise
20+
.then(() => {
21+
assert.htmlEqual(target.innerHTML, `
22+
<p>loaded</p>
23+
`);
24+
25+
let reject;
26+
27+
promise = new Promise((f, r) => {
28+
reject = r;
29+
});
30+
31+
component.promise = promise;
32+
33+
assert.htmlEqual(target.innerHTML, `
34+
<p>loading...</p>
35+
`);
36+
37+
reject(new Error('this error should be thrown'));
38+
return promise;
39+
})
40+
.catch((err) => {
41+
assert.equal(err.message, 'this error should be thrown');
42+
});
43+
}
44+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
export let promise;
3+
</script>
4+
5+
{#await promise}
6+
<p>loading...</p>
7+
{:then value}
8+
<p>loaded</p>
9+
{/await}

0 commit comments

Comments
 (0)