Skip to content

ChimericEagerAsyncFactory

Provided by @chimeric/react.

Creates an async operation that auto-executes when the component mounts or when parameters change. No manual invoke needed. Useful when dealing with promises that you want to invoke immediately, and caching is not necessary.

import { ChimericEagerAsyncFactory } from '@chimeric/react';
const fetchUser = ChimericEagerAsyncFactory({
eagerAsyncFn: async (params: { id: string }) => {
const response = await fetch(`/api/users/${params.id}`);
return response.json();
},
});
const user = await fetchUser({ id: '123' });

Auto-executes immediately. Re-executes when params change.

function UserProfile({ userId }: { userId: string }) {
const { data, isPending, isError, error } = fetchUser.useHook(
{ id: userId },
{ options: { enabled: !!userId } },
);
if (isPending) return <div>Loading...</div>;
if (isError) return <div>Error: {error?.message}</div>;
return <div>{data?.name}</div>;
}

Return value:

PropertyType
dataTResult | undefined
isPendingboolean
isIdleboolean
isSuccessboolean
isErrorboolean
errorTError | null

Reactive options:

OptionTypeDescription
options.enabledbooleanConditionally enable execution (default: true)

IdiomaticEagerAsyncFactory and ReactiveEagerAsyncFactory are also available for single-path usage.