Skip to content

Commit d8ca0e3

Browse files
authored
Merge pull request #1379 from sveltejs/gh-1251
preserve outer context for await blocks
2 parents ed605bf + 5fd4965 commit d8ca0e3

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/compile/nodes/AwaitBlock.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export default class AwaitBlock extends Node {
9898
block.addVariable(promise);
9999
block.addVariable(resolved);
100100

101+
block.maintainContext = true;
102+
101103
// the `#component.root.set({})` below is just a cheap way to flush
102104
// any oncreate handlers. We could have a dedicated `flush()` method
103105
// but it's probably not worth it
@@ -119,21 +121,19 @@ export default class AwaitBlock extends Node {
119121
}
120122
}
121123
122-
function ${handle_promise}(${promise}, ctx) {
124+
function ${handle_promise}(${promise}) {
123125
var ${token} = ${await_token} = {};
124126
125127
if (@isPromise(${promise})) {
126128
${promise}.then(function(${value}) {
127129
${this.value ? deindent`
128-
var ctx = #component.get();
129130
${resolved} = { ${this.value}: ${value} };
130131
${replace_await_block}(${token}, ${create_then_block}, @assign(@assign({}, ctx), ${resolved}));
131132
` : deindent`
132133
${replace_await_block}(${token}, null, null);
133134
`}
134135
}, function (${error}) {
135136
${this.error ? deindent`
136-
var ctx = #component.get();
137137
${resolved} = { ${this.error}: ${error} };
138138
${replace_await_block}(${token}, ${create_catch_block}, @assign(@assign({}, ctx), ${resolved}));
139139
` : deindent`
@@ -155,7 +155,7 @@ export default class AwaitBlock extends Node {
155155
}
156156
}
157157
158-
${handle_promise}(${promise} = ${snippet}, ctx);
158+
${handle_promise}(${promise} = ${snippet});
159159
`);
160160

161161
block.builders.create.addBlock(deindent`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
let fulfil;
2+
3+
let thePromise = new Promise(f => {
4+
fulfil = f;
5+
});
6+
7+
const items = [{
8+
title: 'a title',
9+
data: thePromise
10+
}];
11+
12+
export default {
13+
data: {
14+
items
15+
},
16+
17+
html: `
18+
<p>a title: loading...</p>
19+
`,
20+
21+
test(assert, component, target) {
22+
fulfil(42);
23+
24+
return thePromise
25+
.then(() => {
26+
assert.htmlEqual(target.innerHTML, `
27+
<p>a title: 42</p>
28+
`);
29+
});
30+
}
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{#each items as item}
2+
{#await item.data}
3+
<p>{item.title}: loading...</p>
4+
{:then result}
5+
<p>{item.title}: {result}</p>
6+
{/await}
7+
{/each}

0 commit comments

Comments
 (0)