timed-fetch
Description
Isomorphic fetch wrapper that allows the setting of a max wait-time for a response, if the wait-time is exceeded a 408 Request Timeout HTTP Status Code will be sent back. This allows developers to handle excessivly long API calls without making the user wait, whether the issue stems from a bad internet connection, or a slow backend service.
Code Example
import timedFetch from 'timed-fetch'
const fetchDataWith4SecondLimit = async () => {
const options = {
timeout: 4, // wait 4 seconds max for fetch response.
method: "GET",
}
try {
const response = await timedFetch("https://example.com/api", options)
const json = response.json()
return json
} catch (err) {
return new Error(err.message)
}
}