Skip to main content

Parallel requests

A dashboard with three cards: users, posts and todos. Each card has its own request, and the API answers them with different delays, so you can watch the cards fill in one by one while a global indicator counts what is still loading.

beginneruseFetchcache
import { createClient } from "@hyper-fetch/core";

export const client = createClient({ url: "https://dummyjson.com" });

/** dummyjson waits `delay` ms before it answers, so every card finishes at a different moment. */
type ListQuery = { limit: number; select: string; delay: number };

export type User = { id: number; firstName: string; lastName: string };
export type Post = { id: number; title: string };
export type Todo = { id: number; todo: string; completed: boolean };

export const getUsers = client.createRequest<{
response: { users: User[]; total: number };
queryParams: ListQuery;
}>()({ endpoint: "/users" });

export const getPosts = client.createRequest<{
response: { posts: Post[]; total: number };
queryParams: ListQuery;
}>()({ endpoint: "/posts" });

export const getTodos = client.createRequest<{
response: { todos: Todo[]; total: number };
queryParams: ListQuery;
}>()({ endpoint: "/todos" });
Run locallynpx tiged BetterTyped/hyper-fetch/examples/react/parallel-requests hyper-fetch-react-parallel-requests && cd hyper-fetch-react-parallel-requests && npm install && npm run dev
What it shows
  1. Parallel by default: several useFetch calls in one component start together, with no waterfall.
  2. Independent states: every card reads its own loading, error and data, so a slow request never blocks a fast one.
  3. Global indicator: useIsFetching(client) returns how many fetch requests are running right now.
  4. Refresh one or all: refetch() sends one request again; calling it on every hook refreshes the whole screen.

How it works​

  1. api.ts defines getUsers, getPosts and getTodos. Their queryParams include delay, which dummyjson uses to wait before it answers. The example sets 600, 1400 and 2200 ms so the order is easy to see.

  2. App calls useFetch three times. Hooks do not wait for each other: each one adds its request to the client's fetch dispatcher on mount, and the browser sends all three at once.

    const users = useFetch(
    getUsers.setQueryParams({
    limit: 3,
    // `select` and `delay` are dummyjson query params: return only these fields, answer after `delay` ms.
    select: "firstName,lastName",
    delay: 600,
    }),
    );
    const posts = useFetch(getPosts.setQueryParams({ limit: 3, select: "title", delay: 1400 }));
  3. Card gets the loading and error of one hook only. Show a placeholder per section instead of one spinner for the whole page.

  4. GlobalIndicator calls useIsFetching(client). It re-renders only when the number of running requests changes, so it is cheap to keep in a header or a top bar.

  5. Refresh all calls refetch() on each hook. A manual refetch() always goes to the network, even when the cached data is still fresh.

When to use it
  • Dashboards and overview pages built from several unrelated endpoints.
  • Any screen where one slow endpoint should not hold back the rest.
  • A global "loading" bar or favicon badge driven by useIsFetching (or useIsSubmitting for saves).

Summary

Each useFetch call is an independent request with its own state, and several of them run in parallel. useIsFetching gives you one number for everything still in flight.