Async values

6 min·Assumes: state, computed, conditionals

Some data takes time to load. You choose what the page shows while it waits.

import { computed, state } from '@markless/core';
import { fetchStick } from './dog.ts';

export default function Park() @{
	let throws = state(1);
	const stick = computed(async () => fetchStick(throws));

	<section>
		@try {
			<p>Rex brought back {stick}</p>
		} @pending {
			<p>Rex is still running</p>
		}
		<button onClick={() => throws++}>
			Throw it again
		</button>
	</section>
}

fetchStick is slow. Put it in computed(async …), and @pending shows while it loads. When the answer is ready, @try shows it.

Show something while you wait

Data that takes a second to load shouldn't leave a hole in the page. Put what to show while it loads in @pending. The rest of the page carries on as normal.

PlaygroundRestored your edit

In the corridor

Try this
  1. To do: Walk the knight into the red door with the arrow keys, or hold the arrows under the scene.
  2. To do: Walk him back out and into the red door again: no loading this time. Then try the blue door.
  3. To do: In rooms.ts, change the blue door's tip to Tip: knock first.

The first answer comes with the page

A page that opens on loading screens makes you wait before you can read anything. Markless loads the first answer before the page is shown, so the page opens complete. You only wait for the answers after it.

An ink drawing of a theatre stage seen from the stalls, over the backs of the audience's heads. The purple curtain is up, and scene 1 is already set: a cut-out tree on a wheeled wagon. A card on the arch shows the scene number. Press GO on the cue box at the left: a stagehand in black wheels the next set piece on from the side while the cue light glows, and after a short wait he swaps it in for the old one, in front of you. Pull the rope at the right: the curtain comes down, the stagehand's striped socks show under it while he sets the stage, and it goes up again on scene 1, already set.

The old answer stays until the new one is ready

Asking for a new answer usually means losing the one you were looking at while a loading screen takes its place. Markless keeps the last answer on screen until the new one is ready, so the page never goes back to empty.

Call base twice. Her first reply stays, faded, until the second comes through. Markless marks the old reply with aria-busy="true" while it waits, and the CSS fades anything with that mark.

PlaygroundRestored your edit

Fort to base. Come in. Over.

Base here. Over.

Try this
  1. To do: Hold to talk, then let go. Your message shows at once, and base’s last answer stays, crackling, until the new one comes through.
  2. To do: In base.ts, change 500 to 100. Base answers sooner, so the crackle is shorter.

Only the newest answer is shown

Sometimes an answer to an older request arrives after a newer one. Markless never shows it. The page keeps the answer to your latest request.

Press the planes to throw them. Plane 1 leaves first but takes the long way round. Plane 2 leaves second and lands first. The window keeps plane 2 and turns plane 1 away.

An ink drawing of two brick houses face to face across a street. In your window, two paper planes sit on the sill: an odd number, which loops the long way, and the next even number, which flies straight across. Opposite is a sash window with a pinboard behind it. Press the planes, or focus them and press Enter, to throw both: the window opposite opens, the straight plane gets there first and is pinned up, and the window shuts. The looping plane arrives later, bounces off the glass and falls to the street. Escape puts everything back.

The older request can be cancelled too, so it stops using the network. The browser's way to cancel a fetch is an AbortSignal. Markless hands your function one, named signal, and cancels it when a newer run starts. Pass it to fetch:

const forecast = computed(async ({ signal }) => {
	const response = await fetch(
		`/api/forecast?city=${city}`,
		{ signal },
	);
	return response.json();
});

When loading fails, say so and try again

Loading can fail: the connection drops, or the server says no. When it does, @catch shows, and the last answer stays on the page. Call reset to try again.

PlaygroundRestored your edit

Duck

Try this
  1. To do: Steer the claw over the toast and press Drop.
  2. To do: Press Insert coin until the toast is yours. The duck stays in the chute until then.
  3. To do: In machine.ts, change goes % 3 to goes % 2: the claw holds on every other go.

An error thrown by your own async function inside the @try shows in @catch too.

Do two slow values wait for each other?

No. Say the weather loads in one second and the news in three. Give each its own @try:

@try {
	<p>{weather}</p>
} @pending {
	<p>Loading the weather</p>
}

@try {
	<p>{news}</p>
} @pending {
	<p>Loading the news</p>
}

The weather shows after one second. The news shows two seconds later.

Coming from React?

@pending does the job of a Suspense fallback and @catch the job of an error boundary, written where the value is shown. There is no loading flag to keep in state and no effect that starts the request: reading the value is enough.

Styling shows how a component holds its own CSS.