1.0.0 • Published 2 years ago

timeout-fetch v1.0.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
2 years ago

Timeout Fetch

Creates an instance of fetch that aborts after a certain time period.

import timeoutFetch, { TimeoutError } from 'timeout-fetch';
import nodeFetch from 'node-fetch';

// Timeout is defined in miliseconds
const ONE_SECOND = 1000;
const fetch = timeoutFetch(
	ONE_SECOND,
	nodeFetch // Defaults to globalThis.fetch if you don't provide a fetch implementation yourself
);

try {
	await fetch('http://example.com');
	console.log('Fetch success');
} catch(e) {
	if(e instanceof TimeoutError) {
		console.log('Fetch timed out');
	} else {
		console.log('Another error occured');
	}
}

Dependencies

None

timeout-fetch

Timeout Fetch

See: module:timeout-fetch.default

module.exports(count, fetch) ⇒ function

Creates a fetch function that is aborted after a timeout period

Kind: Exported function
Returns: function - The created fetch function. Has the same signature as the passed param.

ParamTypeDescription
countnumberThe timeout duration in miliseconds
fetchfunctionThe fetch function to call, defaults to the global fetch

module.exports.TimeoutError

Error that is thrown when the timeout expires

Kind: static class of module.exports