about summary refs log tree commit diff stats
path: root/frontend/static
diff options
context:
space:
mode:
authorAlan Pearce2024-05-12 12:57:41 +0200
committerAlan Pearce2024-05-12 12:57:41 +0200
commit00ff3a75357d295d2f7432888a146954ed9f0b7a (patch)
treed044246a2bb58992f7c7de700589b1352e801ff7 /frontend/static
parent68cd867bf76a0bfe6c07c13acb25ac231de4998f (diff)
downloadsearchix-00ff3a75357d295d2f7432888a146954ed9f0b7a.tar.lz
searchix-00ff3a75357d295d2f7432888a146954ed9f0b7a.tar.zst
searchix-00ff3a75357d295d2f7432888a146954ed9f0b7a.zip
refactor(js): use async/await, it's 2024
Diffstat (limited to 'frontend/static')
-rw-r--r--frontend/static/search.js50
1 files changed, 23 insertions, 27 deletions
diff --git a/frontend/static/search.js b/frontend/static/search.js
index a65e10c..15cabe2 100644
--- a/frontend/static/search.js
+++ b/frontend/static/search.js
@@ -64,34 +64,30 @@ function renderFragmentHTML(html) {
   }
 }
 
-function getResults(url) {
-  fetch(url, {
-    headers: {
-      fetch: "true",
-    },
-  })
-    .then(async function (res) {
-      if (res.ok) {
-        return res.text();
-      } else {
-        throw new Error(`${res.status} ${res.statusText}: ${await res.text()}`);
-      }
-    })
-    .then(function (html) {
-      state.fragment = html;
-      history.pushState(state, null, url);
-      return renderFragmentHTML(html);
-    })
-    .catch(function (error) {
-      range.deleteContents();
-      range.insertNode(new Text(error.message));
-      console.error("fetch failed", error);
-    })
-    .finally(function () {
-      state.url = url.toJSON();
-      state.opened = [];
-      history.pushState(state, null, url);
+async function getResults(url) {
+  try {
+    const res = await fetch(url, {
+      headers: {
+        fetch: "true",
+      },
     });
+
+    if (res.ok) {
+      state.fragment = await res.text();
+      history.pushState(state, null, url);
+      renderFragmentHTML(state.fragment);
+    } else {
+      throw new Error(`${res.status} ${res.statusText}: ${await res.text()}`);
+    }
+  } catch (error) {
+    range.deleteContents();
+    range.insertNode(new Text(error.message));
+    console.error("fetch failed", error);
+  } finally {
+    state.url = url.toJSON();
+    state.opened = [];
+    history.pushState(state, null, url);
+  }
 }
 
 search.addEventListener("submit", function (ev) {