Skip to content

resource()

Fetch async data with reactive loading, error, and data states.

function resource<T>(fetcher: () => Promise<T>): Resource<T>
ParamTypeDescription
fetcher() => Promise<T>Async function that returns data
PropertyTypeDescription
.data()T | undefinedResolved data
.loading()booleanTrue while fetching
.error()Error | undefinedRejection error
.refetch()voidRe-execute the fetcher
import { resource, div, p, ul, li, when, each } from "@whisq/core";
const users = resource(() =>
fetch("/api/users").then(r => r.json())
);
div(
when(() => users.loading(), () => p("Loading...")),
when(() => !!users.error(), () => p(() => users.error()!.message)),
when(() => !!users.data(), () =>
ul(each(() => users.data()!, (u) => li(u.name)))
),
)