Skip to content

Latest commit

 

History

History
43 lines (30 loc) · 1.29 KB

README.md

File metadata and controls

43 lines (30 loc) · 1.29 KB

Rune Query

A library for handling asynchronous functions with Runes

.github/workflows/test.yaml

Usage

// in your svelte component or .svelte.js/ts files
let { query, invalidate } = createQuery(async (args) => { .../* some data to be fetched */ });

let q = query({ id: 123 });

// loading is true initially until the asynchronous function completes
q.loading;

// The data can be found in the data key
q.data;

// If an error is thrown in your function, it will be available under the error key
q.error;

// later, you may wish to invalidate the current data, and refetch
invalidate();
// That's it, the reactive `data` and `loading` values above will reactively update

Type inference

The type for data is inferred from the ReturnType of your async function. This means no manually typing expected types etc.

let { query } = createQuery(async () => 123);
query.data; // number | undefined
let { query } = createQuery(async () => "hello world");
query.data; // string | undefined

// when loading is complete, the data type will resolve to just the type (not undefined anymore)
if (!query.loading) {
  query.data; // string
}