Skip to content

Commit

Permalink
await: rethrow error when there is no catch block, fixes sveltejs#5129
Browse files Browse the repository at this point in the history
  • Loading branch information
irshadshalu committed Jul 14, 2020
1 parent fc7e99e commit e297182
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/runtime/internal/await_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export function handle_promise(promise, info) {
update(info.then, 1, info.value, value);
set_current_component(null);
}, error => {
if (info.current === info.catch && info.error === undefined) {
// when no catch block is present
throw error;
}
set_current_component(current_component);
update(info.catch, 2, info.error, error);
set_current_component(null);
Expand Down
44 changes: 44 additions & 0 deletions test/runtime/samples/await-without-catch/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
let fulfil;

let promise = new Promise(f => {
fulfil = f;
});

export default {
props: {
promise
},

html: `
<p>loading...</p>
`,

test({ assert, component, target }) {
fulfil(42);

return promise
.then(() => {
assert.htmlEqual(target.innerHTML, `
<p>loaded</p>
`);

let reject;

promise = new Promise((f, r) => {
reject = r;
});

component.promise = promise;

assert.htmlEqual(target.innerHTML, `
<p>loading...</p>
`);

reject(new Error('this error should be thrown'));
return promise;
})
.catch((err) => {
assert.equal(err.message, 'this error should be thrown');
});
}
};
9 changes: 9 additions & 0 deletions test/runtime/samples/await-without-catch/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
export let promise;
</script>

{#await promise}
<p>loading...</p>
{:then value}
<p>loaded</p>
{/await}

0 comments on commit e297182

Please sign in to comment.