# Loading and errors

## Loading values

`@await (val name = expression)` runs an expression that can call `suspend` functions, and its first block shows the result. `@pending { }` shows while it
loads, and `@catch (e) { }` when it throws, both optional. Without a value to name, the expression stands alone: `@await (save(draft)) { Saved }`.

```html
@await (val user = loadUser(id)) {
	<p>{user.name}</p>
} @pending {
	<p>Loading...</p>
} @catch (e) {
	<p>Error: {e.message}</p>
}
```

This one asks an API route of this site for its sections of the documentation, and shows another one on each click:


```html
<script kotlin>
	import io.github.ayfri.lithe.kobweb.apiData
	import website.model.Tip

	var round by state(0)
</script>

@await (val tips = apiData<List<Tip>>("tips")) {
	@if (val tip = tips.getOrNull(round * 7919 % tips.size)) {
		<blockquote>{tip.text}</blockquote>
	}
} @pending {
	<p>Loading the sections…</p>
} @catch (error) {
	<p>No tip this time: {error.message}</p>
}
<button type="button" @click={round++}>Another one</button>
```

### When it runs again

The expression runs again when something it read before its first suspension changes, here `round`. The `@pending` block takes the place of the content,
or the content stays until the new value arrives when there is no `@pending`. If an older run finishes late, its result is ignored.

`Await` uses the coroutine support of the standard library, so the runtime needs no coroutine library.

### At build time

During static rendering, the build waits for the value and writes the first block into the HTML (or the `@catch` block when it throws), so the data is in the
page for crawlers. Without `@catch`, **the build fails**. The expression runs on the JVM there, so it can't use a browser API: a loader that differs between
the two platforms is declared once in `src@jvm` and once in `src@js`.

### In the browser

A page that was rendered statically keeps what the build wrote while the expression runs again, then builds the block with the new value in its place.
Until then that part of the page is plain HTML: it has no events and no bindings, and the focus or the scroll position inside it don't survive the
replacement. An `@await` inside another one starts again once the outer one has built its block. `@pending` only shows for content the browser creates.
When the expression throws and there is no `@catch`, what shows stays and the error is reported in the console.

`@await` isn't possible inside `<textarea>`, `<title>` and `<head>`.

## Error boundaries

`@try { }` shows its content, and the `@catch { }` after it shows in its place when building or updating the content throws. Without a `@catch`, nothing
shows in its place: `@try { <Chart {data} /> }` skips the content that fails. The head of the `@catch` names the error and the function that builds the
content again, both optional:

```html
@try {
	<Chart {data} />
} @catch { error, retry ->
	<p>The chart failed: {error.message}</p>
	<button @click={retry()}>Try again</button>
}
```

The gauge below throws past 100. Drag the slider past it and break it on purpose, then retry:


```html
<!-- Gauge.lithe -->
<script kotlin>
	prop val value: Int

	val percent by derived {
		check(value in 0..100) { "a gauge reads 0 to 100, not $value" }
		value
	}
</script>

<p class="reading">{percent} %</p>
```

```html
<!-- Boundary.lithe -->
<script kotlin>
	var level by state(60.0)
</script>

<input type="range" min="0" max="150" valueAsNumber={::level}>
@try {
	<Gauge value={level.toInt()} />
} @catch { error, retry ->
	<p>The gauge failed: {error.message}</p>
	<button type="button" @click={level = 100.0; retry()}>Set it to 100 and retry</button>
}
```

- It takes what throws while the content builds, and what an effect of the content throws while the page updates, a text or an attribute that reads a value
  that throws included. What the rest of the page does stays: only the content leaves, and a sibling is never touched.
- It doesn't take what an event handler throws, nor what a `mount` block or a `@mount` throws once the page is up.
- `retry()` builds the content again from scratch. If that throws again, the fallback shows again. Static rendering has no use for it: it does nothing
  there.
- When the content throws while the page is rendered at build time, the page holds the fallback (nothing without a `@catch`), and the browser builds the
  content again: if that works, the content takes the place of the fallback. An error in the fallback itself goes up to the next `@try`.
- `error` is a `Throwable` read through an accessor, like the error of an `@await`. `@try` isn't possible in `<head>`.
