From e447e9c9d76af97cef42ad7e24ddb9250d579b58 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 21 Feb 2024 15:23:12 -0800 Subject: fix back button, abort long requests --- src/lib/utils.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/lib/utils.js') diff --git a/src/lib/utils.js b/src/lib/utils.js index 53437f56..d3ad0da3 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -41,16 +41,24 @@ export let get = (url, headers = {}) => { }); }; -export let post = (url, body, headers) => { +export let post = async (url, body, headers, timeout = 8000) => { headers = { - ...headers, "content-type": "application/json", accept: "application/json", + ...headers, }; - return fetch(base + url, { + + let controller = new AbortController(); + console.log(controller, timeout); + let id = setTimeout(() => { + controller.abort(new Error("Operation timed out")); + }, timeout); + + let response = await fetch(base + url, { method: "POST", body: JSON.stringify(body), headers, + signal: controller.signal, }) .then(async (r) => { if (r.ok) return r.text(); @@ -77,6 +85,9 @@ export let post = (url, body, headers) => { return body; }); + + clearTimeout(id); + return response; }; export let copy = (text) => { -- cgit v1.2.3