4cd60da7b8
- Removed `success` store as it wouldn't work - We should check for a value in error instead
28 lines
734 B
TypeScript
28 lines
734 B
TypeScript
import { Readable, readable } from "svelte/store";
|
|
|
|
interface AsyncData<T, E> {
|
|
value: Readable<T | null>;
|
|
error: Readable<E | null>;
|
|
loading: Readable<boolean>;
|
|
}
|
|
|
|
function useAsync<T, E = unknown>(asyncFunction: () => Promise<T>): AsyncData<T, E> {
|
|
const promise = asyncFunction();
|
|
|
|
const value = readable(null, (set: (value: T) => void) => {
|
|
promise.then((value: T) => set(value));
|
|
});
|
|
|
|
const error = readable(null, (set: (value: E) => void) => {
|
|
promise.catch((value: E) => set(value));
|
|
});
|
|
|
|
const loading = readable(true, (set: (value: boolean) => void) => {
|
|
promise.finally(() => set(false));
|
|
});
|
|
|
|
return { value, error, loading };
|
|
}
|
|
|
|
export default useAsync;
|