-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] allow shorthand {#await ... then/catch} (#6564)
- Loading branch information
Showing
5 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
let fulfil; | ||
|
||
let thePromise = new Promise(f => { | ||
fulfil = f; | ||
}); | ||
|
||
export default { | ||
props: { | ||
thePromise | ||
}, | ||
|
||
html: ` | ||
<br /> | ||
<p>the promise is pending</p> | ||
`, | ||
|
||
async test({ assert, component, target }) { | ||
fulfil(42); | ||
|
||
await thePromise; | ||
|
||
assert.htmlEqual(target.innerHTML, '<br />'); | ||
|
||
let reject; | ||
|
||
thePromise = new Promise((f, r) => { | ||
reject = r; | ||
}); | ||
|
||
component.thePromise = thePromise; | ||
|
||
assert.htmlEqual(target.innerHTML, ` | ||
<br /> | ||
<p>the promise is pending</p> | ||
`); | ||
|
||
reject(new Error()); | ||
|
||
await thePromise.catch(() => {}); | ||
|
||
assert.htmlEqual(target.innerHTML, ` | ||
<p>oh no! Something broke!</p> | ||
<br /> | ||
<p>oh no! Something broke!</p> | ||
`); | ||
} | ||
}; |
15 changes: 15 additions & 0 deletions
15
test/runtime/samples/await-catch-no-expression/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script> | ||
export let thePromise; | ||
</script> | ||
|
||
{#await thePromise catch} | ||
<p>oh no! Something broke!</p> | ||
{/await} | ||
|
||
<br /> | ||
|
||
{#await thePromise} | ||
<p>the promise is pending</p> | ||
{:catch} | ||
<p>oh no! Something broke!</p> | ||
{/await} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
let fulfil; | ||
|
||
let thePromise = new Promise(f => { | ||
fulfil = f; | ||
}); | ||
|
||
export default { | ||
props: { | ||
thePromise | ||
}, | ||
|
||
html: ` | ||
<br> | ||
<br> | ||
<p>the promise is pending</p> | ||
`, | ||
|
||
async test({ assert, component, target }) { | ||
fulfil(); | ||
|
||
await thePromise; | ||
|
||
assert.htmlEqual(target.innerHTML, ` | ||
<p>the promise is resolved</p> | ||
<br> | ||
<p>the promise is resolved</p> | ||
<br> | ||
<p>the promise is resolved</p> | ||
`); | ||
|
||
let reject; | ||
|
||
thePromise = new Promise((f, r) => { | ||
reject = r; | ||
}); | ||
|
||
component.thePromise = thePromise; | ||
|
||
assert.htmlEqual(target.innerHTML, ` | ||
<br> | ||
<br> | ||
<p>the promise is pending</p> | ||
`); | ||
|
||
reject(new Error('something broke')); | ||
|
||
await thePromise.catch(() => {}); | ||
|
||
assert.htmlEqual(target.innerHTML, ` | ||
<p>oh no! something broke</p> | ||
<br> | ||
<br> | ||
`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<script> | ||
export let thePromise; | ||
</script> | ||
|
||
{#await thePromise then} | ||
<p>the promise is resolved</p> | ||
{:catch theError} | ||
<p>oh no! {theError.message}</p> | ||
{/await} | ||
|
||
<br /> | ||
|
||
{#await thePromise then} | ||
<p>the promise is resolved</p> | ||
{/await} | ||
|
||
<br /> | ||
|
||
{#await thePromise} | ||
<p>the promise is pending</p> | ||
{:then} | ||
<p>the promise is resolved</p> | ||
{/await} |