[feat] allow shorthand {#await ... then/catch} (#6564)

pull/6593/head
Tan Li Hau 3 years ago committed by GitHub
parent 32b376b472
commit e1d0d00ebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -290,16 +290,24 @@ export default function mustache(parser: Parser) {
const await_block_shorthand = type === 'AwaitBlock' && parser.eat('then');
if (await_block_shorthand) {
parser.require_whitespace();
block.value = read_context(parser);
parser.allow_whitespace();
if (parser.match_regex(/\s*}/)) {
parser.allow_whitespace();
} else {
parser.require_whitespace();
block.value = read_context(parser);
parser.allow_whitespace();
}
}
const await_block_catch_shorthand = !await_block_shorthand && type === 'AwaitBlock' && parser.eat('catch');
if (await_block_catch_shorthand) {
parser.require_whitespace();
block.error = read_context(parser);
parser.allow_whitespace();
if (parser.match_regex(/\s*}/)) {
parser.allow_whitespace();
} else {
parser.require_whitespace();
block.error = read_context(parser);
parser.allow_whitespace();
}
}
parser.eat('}', true);

@ -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…
Cancel
Save