mirror of https://github.com/sveltejs/svelte
[feat] allow shorthand {#await ... then/catch} (#6564)
parent
32b376b472
commit
e1d0d00ebb
@ -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>
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
};
|
@ -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}
|
@ -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>
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
};
|
@ -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}
|
Loading…
Reference in new issue