Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

simonplend/fetch-with-timeout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fetch-with-timeout

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?

Requirements

  • Node.js >= v16.0.0

Note: This library won't be needed in future versions of Node.js which support AbortSignal.timeout.

Installation

npm install github:simonplend/fetch-with-timeout

Usage

/**
 * 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);
}

TODO

  • 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

About

Cancels HTTP requests with the Abort API. Bring your own Fetch API implementation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published