#Should we use async keyword when declaring query/mutation functions?

2 messages · Page 1 of 1 (latest)

fast berry
#

const useFetchPosts = () => {
  async function fetchPosts() {
    const url = "http://localhost:8080/api/post";
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error("Network response was not ok");
    }
    const result = await response.json();
    return result;
  }
  const { status, data, error } = useQuery({
    queryKey: ["posts"],
    queryFn: fetchPosts,
  });
  return { status, data, error };
};
const { status, data, error } = useFetchPosts();

I saw this style of creating a "custom" hook in a stackoverflow forum. I have a question, should I declare the custom hook as an async function? Query function etc, should they be declare as async functions then await where needed or tanstack take care of that for us?

idle hare