Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

async-fetch

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-fetch

A React hook for async fetch requests.

latest
Source
npmnpm
Version
0.4.0
Version published
Maintainers
1
Created
Source

async-fetch

A React hook for async fetch requests with built-in state management, cancellation, and polling.

Installation

npm install async-fetch
# or
yarn add async-fetch

Usage

import useAsyncFetch from "async-fetch";

function App() {
  const { pending, data, error, sendRequest, cancelRequest } = useAsyncFetch(
    "https://jsonplaceholder.typicode.com/todos/1",
  );

  return (
    <React.Fragment>
      <button onClick={sendRequest}>Send request</button>
      <button onClick={cancelRequest} disabled={!pending}>
        Cancel request
      </button>
      {pending
        ? "Loading..."
        : data
        ? JSON.stringify(data)
        : error
        ? JSON.stringify(error)
        : ""}
    </React.Fragment>
  );
}

Auto-fetch on mount

By default the hook fires on mount. Set auto: false to disable:

const { pending, data, sendRequest } = useAsyncFetch(url, { auto: false });

POST with JSON body

const { pending, data, error } = useAsyncFetch("/api/submit", {
  method: "POST",
  data: { name: "foo" },
});

Polling

const { data } = useAsyncFetch("/api/status", { poll: 5000 });

Callbacks

useAsyncFetch("/api/user/1", {
  onStart: () => console.log("started"),
  onSuccess: (data) => console.log(data),
  onFail: (error) => console.error(error),
  onFinish: () => console.log("finished"),
});

Cancel on demand

const { cancelRequest } = useAsyncFetch("/api/user/1");
<button onClick={cancelRequest}>Cancel</button>;

Request options

The minimum requirement is a URL string as the first argument. The second argument accepts the following options — anything else is passed directly to the underlying fetch call.

OptionTypeDefaultDescription
initialPendingbooleanfalseInitial state for pending
initialErrorEInitial state for error
initialDataTInitial state for data
autobooleantrueWhether to auto-send the request on mount
pollnumberMilliseconds between polling requests
timeoutnumber30000Milliseconds before the request is cancelled
ignoreRequestbooleanSkips sending the request when true
ignoreCleanupbooleanSkips cancelling the request on unmount when true
queryobjectKey-value pairs appended to the URL as search params
paramsobjectKey-value pairs appended to the URL as search params
dataunknownValue to send as the request body
parser"json" | "text" | "blob" | "formData" | "arrayBuffer""json"Method used to parse the response
onStart() => voidCalled when the request starts
onSuccess(data: T) => voidCalled with the response data on success
onFail(error: E) => voidCalled with the error on failure
onFinish() => voidCalled when the request finishes

Response

PropertyTypeDescription
pendingbooleanWhether the request is active
errorEThe response error
dataTThe response data
sendRequest() => Promise<void>Sends the request manually
cancelRequest() => voidCancels the active request

License

MIT

Keywords

typescript

FAQs

Package last updated on 20 Apr 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts