Basic Svelte
Introduction
Bindings
Advanced Svelte
Advanced reactivity
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion
Most web applications have to deal with asynchronous data at some point. Svelte makes it easy to await the value of promises directly in your markup:
App
{#await promise}
<p>...rolling</p>
{:then number}
<p>you rolled a {number}!</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}
Only the most recent
promise
is considered, meaning you don’t need to worry about race conditions.
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:
{#await promise then number}
<p>you rolled a {number}!</p>
{/await}
previous next
1
2
3
4
5
6
7
8
9
10
11
12
<script>
import { roll } from './utils.js';
let promise = $state(roll());
</script>
<button onclick={() => promise = roll()}>
roll the dice
</button>
<p>...rolling</p>