Skip to content

Commit 9ad4de7

Browse files
authored
Merge pull request #980 from sveltejs/gh-974-975
Fix #974 and #975
2 parents 6641684 + 978e628 commit 9ad4de7

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

src/generators/dom/visitors/AwaitBlock.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default function visitAwaitBlock(
6767
${old_block}.u();
6868
${old_block}.d();
6969
${await_block}.c();
70-
${await_block}.m(${anchor}.parentNode, ${anchor});
70+
${await_block}.m(${state.parentNode || `${anchor}.parentNode`}, ${anchor});
7171
}
7272
}
7373
@@ -142,6 +142,10 @@ export default function visitAwaitBlock(
142142
`);
143143
}
144144

145+
block.builders.unmount.addBlock(deindent`
146+
${await_block}.u();
147+
`);
148+
145149
block.builders.destroy.addBlock(deindent`
146150
${await_token} = null;
147151
${await_block}.d();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
<div><p>loading...</p></div>
14+
`,
15+
16+
test(assert, component, target) {
17+
fulfil(42);
18+
19+
return thePromise
20+
.then(() => {
21+
assert.htmlEqual(target.innerHTML, `
22+
<div><p>the value is 42</p></div>
23+
`);
24+
25+
let reject;
26+
27+
thePromise = new Promise((f, r) => {
28+
reject = r;
29+
});
30+
31+
component.set({
32+
thePromise
33+
});
34+
35+
assert.htmlEqual(target.innerHTML, `
36+
<div><p>loading...</p></div>
37+
`);
38+
39+
reject(new Error('something broke'));
40+
41+
return thePromise.catch(() => {});
42+
})
43+
.then(() => {
44+
assert.htmlEqual(target.innerHTML, `
45+
<div><p>oh no! something broke</p></div>
46+
`);
47+
});
48+
}
49+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div>
2+
{{#await thePromise}}
3+
<p>loading...</p>
4+
{{then theValue}}
5+
<p>the value is {{theValue}}</p>
6+
{{catch theError}}
7+
<p>oh no! {{theError.message}}</p>
8+
{{/await}}
9+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
let fulfil;
2+
3+
const thePromise = new Promise(f => {
4+
fulfil = f;
5+
});
6+
7+
export default {
8+
data: {
9+
show: true,
10+
thePromise
11+
},
12+
13+
html: `
14+
<p>loading...</p>
15+
`,
16+
17+
test(assert, component, target) {
18+
fulfil(42);
19+
20+
return thePromise
21+
.then(() => {
22+
assert.htmlEqual(target.innerHTML, `
23+
<p>the value is 42</p>
24+
`);
25+
26+
component.set({
27+
show: false
28+
});
29+
30+
assert.htmlEqual(target.innerHTML, `
31+
<p>Else</p>
32+
`);
33+
34+
component.set({
35+
show: true
36+
});
37+
38+
return thePromise.then(() => {
39+
assert.htmlEqual(target.innerHTML, `
40+
<p>the value is 42</p>
41+
`);
42+
});
43+
});
44+
}
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{#if show}}
2+
{{#await thePromise}}
3+
<p>loading...</p>
4+
{{then theValue}}
5+
<p>the value is {{theValue}}</p>
6+
{{catch theError}}
7+
<p>oh no! {{theError.message}}</p>
8+
{{/await}}
9+
{{else}}
10+
<p>Else</p>
11+
{{/if}}

0 commit comments

Comments
 (0)