1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00

Using reject here is uncaught, make this a single promise and resolve/reject accordingly (#556)

This commit is contained in:
Bjorn Stromberg 2020-08-15 23:26:54 +09:00 committed by GitHub
parent 90cf65e21e
commit 779ba8a1b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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);
});
});
} }
/** /**