Skip to content

Commit b2a590a

Browse files
author
Rich Harris
committed
tidy up await block example
1 parent f14f11f commit b2a590a

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed

content/tutorial/01-svelte/04-logic/06-await-blocks/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ Most web applications have to deal with asynchronous data at some point. Svelte
66

77
```svelte
88
/// file: App.svelte
9-
{#await promise}
9+
+++{#await promise}+++
1010
<p>...waiting</p>
11-
{:then number}
11+
+++{:then number}
1212
<p>The number is {number}</p>
1313
{:catch error}
1414
<p style="color: red">{error.message}</p>
15-
{/await}
15+
{/await}+++
1616
```
1717

1818
> Only the most recent `promise` is considered, meaning you don't need to worry about race conditions.
1919
2020
If you know that your promise can't reject, you can omit the `catch` block. You can also omit the first block if you don't want to show anything until the promise resolves:
2121

2222
```svelte
23-
/// file: App.svelte
23+
/// no-file
2424
{#await promise then number}
2525
<p>The number is {number}</p>
2626
{/await}
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
<script>
2-
async function getRandomNumber() {
3-
// Fetch a random number between 0 and 100
4-
// (with a delay, so that we can see it)
5-
const res = await fetch('/random-number');
6-
7-
if (res.ok) {
8-
return await res.text();
9-
} else {
10-
// Sometimes the API will fail!
11-
throw new Error('Request failed');
12-
}
13-
}
2+
import { getRandomNumber } from './utils.js';
143
154
let promise = getRandomNumber();
165
@@ -23,5 +12,4 @@
2312
generate random number
2413
</button>
2514

26-
<!-- replace this element -->
27-
<p>{promise}</p>
15+
<p>...waiting</p>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export async function getRandomNumber() {
2+
// Fetch a random number between 0 and 100
3+
// (with a delay, so that we can see it)
4+
const res = await fetch('/random-number');
5+
6+
if (res.ok) {
7+
return await res.text();
8+
} else {
9+
// Sometimes the API will fail!
10+
throw new Error('Request failed');
11+
}
12+
}

content/tutorial/01-svelte/04-logic/06-await-blocks/app-b/src/lib/App.svelte

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
<script>
2-
async function getRandomNumber() {
3-
// Fetch a random number between 0 and 100
4-
// (with a delay, so that we can see it)
5-
const res = await fetch('/random-number');
6-
7-
if (res.ok) {
8-
return await res.text();
9-
} else {
10-
// Sometimes the API will fail!
11-
throw new Error('Request failed');
12-
}
13-
}
2+
import { getRandomNumber } from './utils.js';
143
154
let promise = getRandomNumber();
165

0 commit comments

Comments
 (0)