Skip to main content
Create new
Introduction
Reactivity
Props
Logic
Events
Bindings
Lifecycle
Stores
Motion
Transitions
Animations
Easing
SVG
Actions
Classes
Component composition
Context API
Special elements
Module context
Debugging
7GUIs
Miscellaneous
App.svelte
<script>
let promise = $state(getRandomNumber());

async function getRandomNumber() {
const res = await fetch(`/tutorial/random-number`);
const text = await res.text();

if (res.ok) {
return text;
} else {
throw new Error(text);
}
}

function handleClick() {
promise = getRandomNumber();
}
</script>

<button onclick={handleClick}> generate random number </button>

{#await promise}
<p>...waiting</p>
{:then number}
<p>The number is {number}</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}

loading Svelte compiler...
/* Select a component to see its compiled code */
result = svelte.compile(source, {
generate: ,
fragments: ,
});
/* Select a component to see its compiled code */