Skip to content

ChimericAsyncFactory

Provided by @chimeric/react.

Creates an async operation usable both idiomatically (async/await) and reactively (.useHook() with manual invoke). Useful when dealing with promises and caching is not necessary.

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

The reactive path provides an invoke function. The operation does not auto-execute — you trigger it manually.

function UserProfile({ userId }: { userId: string }) {
const { invoke, data, isPending, isError, error } = fetchUser.useHook();
return (
<div>
<button onClick={() => invoke({ id: userId })}>Load User</button>
{isPending && <div>Loading...</div>}
{isError && <div>Error: {error?.message}</div>}
{data && <div>{data.name}</div>}
</div>
);
}

Return value:

PropertyType
invoke(params, options?) => Promise<TResult>
dataTResult | undefined
isPendingboolean
isIdleboolean
isSuccessboolean
isErrorboolean
errorTError | null

IdiomaticAsyncFactory and ReactiveAsyncFactory are also available for single-path usage.