- Docs
- Components
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 }.
This one asks an API route of this site for its sections of the documentation, and shows another one on each click:
The <script kotlin> block is plain Kotlin. Its top-level statements become the body of the component function, in order, and run once. It goes at the top level of the file, once, and takes no other attribute.
Component files: Script
<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:
The gauge below throws past 100. Drag the slider past it and break it on purpose, then retry:
60 %
<!-- 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
mountblock or a@mountthrows 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. erroris aThrowableread through an accessor, like the error of an@await.@tryisn't possible in<head>.