Refactor the xhr so other requests can reuse the code

This commit is contained in:
Cecylia Bocovich 2023-01-13 14:23:35 -05:00
parent 21a29a09eb
commit e117dc5540
1 changed files with 14 additions and 2 deletions

View File

@ -7,6 +7,19 @@ init().then(() => {
}); });
function request_open_invite() { function request_open_invite() {
return new Promise((fulfill, reject) => {
loxServerRequest("").then((response) => {
console.log("Got invitation token: " + response.invite);
fulfill(response.invite);
return;
}).catch(() => {
console.log("Error requesting open invite from Lox server");
reject();
});
});
}
function loxServerRequest(data) {
return new Promise((fulfill, reject) => { return new Promise((fulfill, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
@ -20,8 +33,7 @@ function request_open_invite() {
return; return;
} }
const response = JSON.parse(xhr.responseText); const response = JSON.parse(xhr.responseText);
console.log("Got invitation token: "+response.invite); fulfill(response);
fulfill(response.invite);
return; return;
}; };
try { try {