1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00

Using reject here is uncaught, make this a single promise and resolve/reject accordingly

This commit is contained in:
Bjorn Stromberg 2020-08-10 11:17:28 +09:00
parent c25428357e
commit f96a9ec67c

View File

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