From 798679ca4490ab80dabd7f38af7b95ac13b8face Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 26 Aug 2023 13:21:08 +0100 Subject: [PATCH] Use async in polltask.js --- app/public/static/js/polltask.js | 90 ++++++++++++++++---------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/app/public/static/js/polltask.js b/app/public/static/js/polltask.js index a1f62c24..a5ff5c23 100644 --- a/app/public/static/js/polltask.js +++ b/app/public/static/js/polltask.js @@ -3,62 +3,62 @@ "use strict"; -function getJSON(url, method) { - return new Promise((resolve, reject) => { - fetch(new Request(url, { + +async function getJSON(url, method) { + const response = await fetch(new Request(url, { method: method || "get", credentials: "same-origin", headers: { "Accept": "application/json", }, - })).then((response) => { - response.text().then((txt) => { - resolve(JSON.parse(txt)) - }).catch(reject) - }).catch(reject) - }) + })); + + return await response.json(); } -function pollTask(poll_url, disableTimeout) { - return new Promise((resolve, reject) => { - let tries = 0; - function retry() { - tries++; - if (!disableTimeout && tries > 30) { - reject("timeout") - } else { - const interval = Math.min(tries*100, 1000) - console.log("Polling task in " + interval + "ms") - setTimeout(step, interval) - } +function sleep(interval) { + return new Promise(resolve => setTimeout(resolve, interval)); +} + + +async function pollTask(poll_url, disableTimeout) { + let tries = 0; + + while (true) { + tries++; + if (!disableTimeout && tries > 30) { + throw "timeout"; + } else { + const interval = Math.min(tries * 100, 1000); + console.log("Polling task in " + interval + "ms"); + await sleep(interval); } - function step() { - getJSON(poll_url).then((res) => { - if (res.status === "SUCCESS") { - console.log("Got result") - resolve(res.result) - } else if (res.status === "FAILURE" || res.status === "REVOKED") { - reject(res.error || "Unknown server error") - } else { - retry() - } - }).catch(retry) + + let res = undefined; + try { + res = await getJSON(poll_url); + } catch (e) { + console.error(e); } - retry() - }) + + if (res && res.status === "SUCCESS") { + console.log("Got result") + return res.result; + } else if (res && (res.status === "FAILURE" || res.status === "REVOKED")) { + throw res.error ?? "Unknown server error"; + } + } } -function performTask(url) { - return new Promise((resolve, reject) => { - getJSON(url, "post").then((startResult) => { - console.log(startResult) - if (typeof startResult.poll_url == "string") { - pollTask(startResult.poll_url).then(resolve).catch(reject) - } else { - reject("Start task didn't return string!") - } - }).catch(reject) - }) +async function performTask(url) { + const startResult = await getJSON(url, "post"); + console.log(startResult); + + if (typeof startResult.poll_url == "string") { + return await pollTask(startResult.poll_url); + } else { + throw "Start task didn't return string!"; + } }