Skip to content

Instantly share code, notes, and snippets.

@alexanderbluhm
Created April 17, 2023 09:23
Show Gist options
  • Save alexanderbluhm/009b1663567afa52350a52c2b22c11b2 to your computer and use it in GitHub Desktop.
Save alexanderbluhm/009b1663567afa52350a52c2b22c11b2 to your computer and use it in GitHub Desktop.
proud code sample
const upload = async (e) => {
e.preventDefault();
setLoading(true)
// ...
try {
const [res] = await Promise.allSettled([
fetch(`${process.env.REACT_APP_API_URL}/upload`, {
method: "POST",
body: data,
credentials: "include",
headers: {
"X-CSRF-TOKEN": Cookies.get("csrf_access_token"),
},
}),
new Promise((resolve) => setTimeout(resolve, 900)),
]);
if (res.value?.ok) {
const body = await res.value?.json();
onNext(body);
} else if (res.value?.status === 401) {
navigate("/login");
} else {
const body = await res.value?.json();
setError(body.message || "An unexpected error occured. Please try again later or reach out to a contact person.");
}
} catch (error) {
setError("An unexpected error occured. Please try again later or reach out to a contact person.");
}
setLoading(false);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment