Skip to content

Commit

Permalink
await...then shorthand - fixes #957
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored Dec 10, 2017
1 parent 81f4490 commit b83afb0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/parse/state/mustache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,22 @@ export default function mustache(parser: Parser) {
}
}

let awaitBlockShorthand = type === 'AwaitBlock' && parser.eat('then');
if (awaitBlockShorthand) {
parser.requireWhitespace();
block.value = parser.readIdentifier();
parser.allowWhitespace();
}

parser.eat('}}', true);

parser.current().children.push(block);
parser.stack.push(block);

if (type === 'AwaitBlock') {
block.pending.start = parser.index;
parser.stack.push(block.pending);
const childBlock = awaitBlockShorthand ? block.then : block.pending;
childBlock.start = parser.index;
parser.stack.push(childBlock);
}
} else if (parser.eat('yield')) {
// {{yield}}
Expand Down
45 changes: 45 additions & 0 deletions test/runtime/samples/await-then-shorthand/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
let fulfil;

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

export default {
data: {
thePromise
},

html: ``,

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

return thePromise
.then(() => {
assert.htmlEqual(target.innerHTML, `
<p>the value is 42</p>
`);

let reject;

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

component.set({
thePromise
});

assert.htmlEqual(target.innerHTML, ``);

reject(new Error('something broke'));

return thePromise.catch(() => {});
})
.then(() => {
assert.htmlEqual(target.innerHTML, `
<p>oh no! something broke</p>
`);
});
}
};
5 changes: 5 additions & 0 deletions test/runtime/samples/await-then-shorthand/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#await thePromise then theValue}}
<p>the value is {{theValue}}</p>
{{catch theError}}
<p>oh no! {{theError.message}}</p>
{{/await}}

0 comments on commit b83afb0

Please sign in to comment.