Skip to content

Commit 6c6233f

Browse files
committed
flesh out await_waterfall message
1 parent 4d8432a commit 6c6233f

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

documentation/docs/98-reference/.generated/client-warnings.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,29 @@ let total = $derived(await sum(a, b));
7171
### await_waterfall
7272
7373
```
74-
An async value (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app
74+
An async derived, `%name%` (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app
7575
```
7676
77-
TODO
77+
In a case like this...
78+
79+
```js
80+
let a = $derived(await one());
81+
let b = $derived(await two());
82+
```
83+
84+
...the second `$derived` will not be created until the first one has resolved. Since `await two()` does not depend on the value of `a`, this delay, often described as a 'waterfall', is unnecessary.
85+
86+
(Note that if the values of `await one()` and `await two()` subsequently change, they can do so concurrently — the waterfall only occurs when the deriveds are first created.)
87+
88+
You can solve this by creating the promises first and _then_ awaiting them:
89+
90+
```js
91+
let aPromise = $derived(one());
92+
let bPromise = $derived(two());
93+
94+
let a = $derived(await aPromise);
95+
let b = $derived(await bPromise);
96+
```
7897
7998
### binding_property_non_reactive
8099

packages/svelte/messages/client-warnings/warnings.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,28 @@ let total = $derived(await sum(a, b));
6464
6565
## await_waterfall
6666
67-
> An async value (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app
67+
> An async derived, `%name%` (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app
6868
69-
TODO
69+
In a case like this...
70+
71+
```js
72+
let a = $derived(await one());
73+
let b = $derived(await two());
74+
```
75+
76+
...the second `$derived` will not be created until the first one has resolved. Since `await two()` does not depend on the value of `a`, this delay, often described as a 'waterfall', is unnecessary.
77+
78+
(Note that if the values of `await one()` and `await two()` subsequently change, they can do so concurrently — the waterfall only occurs when the deriveds are first created.)
79+
80+
You can solve this by creating the promises first and _then_ awaiting them:
81+
82+
```js
83+
let aPromise = $derived(one());
84+
let bPromise = $derived(two());
85+
86+
let a = $derived(await aPromise);
87+
let b = $derived(await bPromise);
88+
```
7089
7190
## binding_property_non_reactive
7291

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export function VariableDeclaration(node, context) {
207207
);
208208

209209
if (is_async) {
210-
const location = dev && is_ignored(init, 'await_waterfall') && locate_node(init);
210+
const location = dev && !is_ignored(init, 'await_waterfall') && locate_node(init);
211211
let call = b.call(
212212
'$.async_derived',
213213
b.thunk(expression, true),

packages/svelte/src/internal/client/reactivity/deriveds.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export function async_derived(fn, location) {
169169

170170
setTimeout(() => {
171171
if (recent_async_deriveds.has(signal)) {
172-
w.await_waterfall(location);
172+
w.await_waterfall(/** @type {string} */ (signal.label), location);
173173
recent_async_deriveds.delete(signal);
174174
}
175175
});

packages/svelte/src/internal/client/warnings.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ export function await_reactivity_loss(name) {
3131
}
3232

3333
/**
34-
* An async value (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app
34+
* An async derived, `%name%` (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app
35+
* @param {string} name
3536
* @param {string} location
3637
*/
37-
export function await_waterfall(location) {
38+
export function await_waterfall(name, location) {
3839
if (DEV) {
39-
console.warn(`%c[svelte] await_waterfall\n%cAn async value (${location}) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app\nhttps://svelte.dev/e/await_waterfall`, bold, normal);
40+
console.warn(`%c[svelte] await_waterfall\n%cAn async derived, \`${name}\` (${location}) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app\nhttps://svelte.dev/e/await_waterfall`, bold, normal);
4041
} else {
4142
console.warn(`https://svelte.dev/e/await_waterfall`);
4243
}

0 commit comments

Comments
 (0)