import { fallback } from "@daiso-tech/core/async";
import { AsyncHooks } from "@daiso-tech/core/utilities";
const fetchData = new AsyncHooks(async (url: string): Promise<unknown> => {
const response = await fetch(url);
const json = await response.json();
if (!response.ok) {
throw json
}
return json;
}, [
fallback({ fallbackValue: null })
]);
// Will return null when the fetch method throws an error.
console.log(await fetchData.invoke("URL_ENDPOINT"));
The
fallback
middleware adds fallback value when an error occurs.IMPORT_PATH:
"@daiso-tech/core/async"