Skip to content

Instantly share code, notes, and snippets.

@obedparla
Created August 24, 2023 11:46
Show Gist options
  • Save obedparla/a473a9986646a310d2217017ba02247b to your computer and use it in GitHub Desktop.
Save obedparla/a473a9986646a310d2217017ba02247b to your computer and use it in GitHub Desktop.
Simple fetch
export function fetchFromUrl<T>(
url: string,
options: RequestInit,
): Promise<T | { error: string }> {
return fetch(url, { ...options })
.then((response) => {
if (response.ok) {
try {
return response.json();
} catch (e) {
return { error: e };
}
}
return { error: response.statusText };
})
.catch((error) => {
return { error: error };
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment