UNPKG

epic-state

Version:

Reactive state management for frontend libraries.

27 lines (20 loc) 670 B
import { state } from '../index' export function load<T>(action: () => Promise<{ error?: boolean | string; data?: T }>) { const container: { loading: boolean; error: boolean | string; data: T } = state({ loading: true, error: false, // Up to the user to ensure loading and error properties are checked before accessing data. data: undefined as unknown as T, }) async function loadData() { const { error, data } = await action() container.loading = false if (error || !data) { container.error = typeof error === 'undefined' ? false : error } else { container.data = data } } loadData() return container }