1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00
This commit is contained in:
tobspr
2020-08-15 17:51:30 +02:00
3 changed files with 534 additions and 522 deletions

View File

@@ -85,10 +85,9 @@ export class ShapezGameAnalytics extends GameAnalyticsInterface {
* @returns {Promise<any>}
*/
sendToApi(endpoint, data) {
return Promise.race([
new Promise((resolve, reject) => {
setTimeout(() => reject("Request to " + endpoint + " timed out"), 20000);
}),
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject("Request to " + endpoint + " timed out"), 20000);
fetch(analyticsUrl + endpoint, {
method: "POST",
mode: "cors",
@@ -103,13 +102,19 @@ export class ShapezGameAnalytics extends GameAnalyticsInterface {
body: JSON.stringify(data),
})
.then(res => {
clearTimeout(timeout);
if (!res.ok || res.status !== 200) {
throw new Error("Fetch error: Bad status " + res.status);
reject("Fetch error: Bad status " + res.status);
} else {
return res.json();
}
return res;
})
.then(res => res.json()),
]);
.then(resolve)
.catch(reason => {
clearTimeout(timeout);
reject(reason);
});
});
}
/**