mirror of
https://github.com/minetest/contentdb.git
synced 2025-01-10 23:17:37 +01:00
Use async in polltask.js
This commit is contained in:
parent
c8a30a27dc
commit
798679ca44
@ -3,62 +3,62 @@
|
|||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
function getJSON(url, method) {
|
|
||||||
return new Promise((resolve, reject) => {
|
async function getJSON(url, method) {
|
||||||
fetch(new Request(url, {
|
const response = await fetch(new Request(url, {
|
||||||
method: method || "get",
|
method: method || "get",
|
||||||
credentials: "same-origin",
|
credentials: "same-origin",
|
||||||
headers: {
|
headers: {
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
},
|
},
|
||||||
})).then((response) => {
|
}));
|
||||||
response.text().then((txt) => {
|
|
||||||
resolve(JSON.parse(txt))
|
return await response.json();
|
||||||
}).catch(reject)
|
|
||||||
}).catch(reject)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function pollTask(poll_url, disableTimeout) {
|
|
||||||
return new Promise((resolve, reject) => {
|
function sleep(interval) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, interval));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function pollTask(poll_url, disableTimeout) {
|
||||||
let tries = 0;
|
let tries = 0;
|
||||||
|
|
||||||
function retry() {
|
while (true) {
|
||||||
tries++;
|
tries++;
|
||||||
if (!disableTimeout && tries > 30) {
|
if (!disableTimeout && tries > 30) {
|
||||||
reject("timeout")
|
throw "timeout";
|
||||||
} else {
|
} else {
|
||||||
const interval = Math.min(tries*100, 1000)
|
const interval = Math.min(tries * 100, 1000);
|
||||||
console.log("Polling task in " + interval + "ms")
|
console.log("Polling task in " + interval + "ms");
|
||||||
setTimeout(step, interval)
|
await sleep(interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let res = undefined;
|
||||||
|
try {
|
||||||
|
res = await getJSON(poll_url);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
}
|
}
|
||||||
function step() {
|
|
||||||
getJSON(poll_url).then((res) => {
|
if (res && res.status === "SUCCESS") {
|
||||||
if (res.status === "SUCCESS") {
|
|
||||||
console.log("Got result")
|
console.log("Got result")
|
||||||
resolve(res.result)
|
return res.result;
|
||||||
} else if (res.status === "FAILURE" || res.status === "REVOKED") {
|
} else if (res && (res.status === "FAILURE" || res.status === "REVOKED")) {
|
||||||
reject(res.error || "Unknown server error")
|
throw res.error ?? "Unknown server error";
|
||||||
} else {
|
|
||||||
retry()
|
|
||||||
}
|
}
|
||||||
}).catch(retry)
|
|
||||||
}
|
}
|
||||||
retry()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function performTask(url) {
|
async function performTask(url) {
|
||||||
return new Promise((resolve, reject) => {
|
const startResult = await getJSON(url, "post");
|
||||||
getJSON(url, "post").then((startResult) => {
|
console.log(startResult);
|
||||||
console.log(startResult)
|
|
||||||
if (typeof startResult.poll_url == "string") {
|
if (typeof startResult.poll_url == "string") {
|
||||||
pollTask(startResult.poll_url).then(resolve).catch(reject)
|
return await pollTask(startResult.poll_url);
|
||||||
} else {
|
} else {
|
||||||
reject("Start task didn't return string!")
|
throw "Start task didn't return string!";
|
||||||
}
|
}
|
||||||
}).catch(reject)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user