Vize

computed-async

Computed value backed by an async evaluator.

Package @vizejs/composable/computed-async
Own the source vize lib pull composable:computed-async
Runtime exports computedAsync
Gzip budget 768 B

Usage

import { computedAsync } from "@vizejs/composable/computed-async";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
computedAsync reactivity experimental safe stable reactive-scope web, server, worker, native, desktop, terminal AbortController —

API

computedAsync

Computed value backed by an async evaluator. Reactive dependencies read synchronously in the evaluator (before its first await) are tracked; when one changes, the previous evaluation is cancelled (its signal aborts and onCancel callbacks run) and a new one starts. Only the newest evaluation may write the result, so out-of-order responses can never win. The value holds initial until the first evaluation settles, which also keeps server and client markup identical during hydration (the server never waits for the promise). Results are stored shallowly: replace the value instead of mutating it.

function computedAsync<Value>( evaluator: (context: ComputedAsyncContext) => Value | Promise<Value>, initial: Value, options?: ComputedAsyncOptions, ): Readonly<Ref<Value>>
const user = computedAsync(
  async ({ signal }) => (await fetch(`/users/${id.value}`, { signal })).json(),
  null,
);

computedAsync

function computedAsync<Value>( evaluator: (context: ComputedAsyncContext) => Value | Promise<Value>, initial?: undefined, options?: ComputedAsyncOptions, ): Readonly<Ref<Value | undefined>>

computedAsync

function computedAsync<Value>( evaluator: (context: ComputedAsyncContext) => Value | Promise<Value>, initial?: Value, options: ComputedAsyncOptions = {}, ): Readonly<Ref<Value | undefined>>

Types

ComputedAsyncContext

Context handed to a computedAsync evaluator.

Member Type Description
signal AbortSignal Aborted as soon as a newer evaluation starts or the scope stops.
onCancel ComputedAsyncCancel Register a callback for when this evaluation becomes stale.

ComputedAsyncOptions

Options for computedAsync.

Member Type Description
lazy? boolean Defer the first evaluation until the value is read.
evaluating? Ref<boolean> Ref that is true while an evaluation is in flight.
onError? (error: unknown) => void Receives evaluator failures; the previous value is kept.