Cancels HTTP requests with the Abort API. Bring your own Fetch API implementation.
🤔 Should I make this a proper package and publish it to npm?
- Node.js >= v16.0.0
Note: This library won't be needed in future versions of Node.js which support AbortSignal.timeout.
npm install github:simonplend/fetch-with-timeout
/**
* This could be any implementation of the Fetch API.
*/
import fetch from "node-fetch";
import { createFetchWithTimeout } from "fetch-with-timeout";
const fetchWithTimeout = createFetchWithTimeout({ fetch, timeout: 100 });
try {
const url = "https://jsonplaceholder.typicode.com/posts";
const response = await fetchWithTimeout(url);
const responseData = await response.json();
console.log({ responseData });
} catch (error) {
console.error(error);
}
- Throw a custom error instance e.g.
FetchTimeout
- Add tests
- Add types
- Improve the README
- Link to James Snell's article
- Link to my 'How to cancel an HTTP request in Node.js' article
- Link to my NodeConf Remote talk
- Publish to npm