add timeout of 15 secs to all fetch requests

This commit is contained in:
Nolan Lawson 2018-02-11 10:49:32 -08:00
parent bea1436711
commit 82e003f7f5

View file

@ -1,5 +1,16 @@
const TIMEOUT = 15000
function fetchWithTimeout(url, options) {
return Promise.race([
fetch(url, options),
new Promise((resolve, reject) => {
setTimeout(() => reject(new Error(`Timed out after ${TIMEOUT / 1000} seconds`)), TIMEOUT)
})
])
}
export async function post (url, body) {
return (await fetch(url, {
return (await fetchWithTimeout(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
@ -18,7 +29,7 @@ export function paramsString (paramsObject) {
}
export async function get (url, headers = {}) {
return (await fetch(url, {
return (await fetchWithTimeout(url, {
method: 'GET',
headers: Object.assign(headers, {
'Accept': 'application/json'