Skip to content

Commit 273452c

Browse files
committed
await...then shorthand - fixes #957
1 parent cf94217 commit 273452c

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/parse/state/mustache.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,22 @@ export default function mustache(parser: Parser) {
283283
}
284284
}
285285

286+
let awaitBlockShorthand = type === 'AwaitBlock' && parser.eat('then');
287+
if (awaitBlockShorthand) {
288+
parser.requireWhitespace();
289+
block.value = parser.readIdentifier();
290+
parser.allowWhitespace();
291+
}
292+
286293
parser.eat('}}', true);
287294

288295
parser.current().children.push(block);
289296
parser.stack.push(block);
290297

291298
if (type === 'AwaitBlock') {
292-
block.pending.start = parser.index;
293-
parser.stack.push(block.pending);
299+
const childBlock = awaitBlockShorthand ? block.then : block.pending;
300+
childBlock.start = parser.index;
301+
parser.stack.push(childBlock);
294302
}
295303
} else if (parser.eat('yield')) {
296304
// {{yield}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
let fulfil;
2+
3+
let thePromise = new Promise(f => {
4+
fulfil = f;
5+
});
6+
7+
export default {
8+
data: {
9+
thePromise
10+
},
11+
12+
html: ``,
13+
14+
test(assert, component, target) {
15+
fulfil(42);
16+
17+
return thePromise
18+
.then(() => {
19+
assert.htmlEqual(target.innerHTML, `
20+
<p>the value is 42</p>
21+
`);
22+
23+
let reject;
24+
25+
thePromise = new Promise((f, r) => {
26+
reject = r;
27+
});
28+
29+
component.set({
30+
thePromise
31+
});
32+
33+
assert.htmlEqual(target.innerHTML, ``);
34+
35+
reject(new Error('something broke'));
36+
37+
return thePromise.catch(() => {});
38+
})
39+
.then(() => {
40+
assert.htmlEqual(target.innerHTML, `
41+
<p>oh no! something broke</p>
42+
`);
43+
});
44+
}
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{#await thePromise then theValue}}
2+
<p>the value is {{theValue}}</p>
3+
{{catch theError}}
4+
<p>oh no! {{theError.message}}</p>
5+
{{/await}}

0 commit comments

Comments
 (0)