From e117dc554036a63bc96f38a3a5039c4f6e3bfc45 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Fri, 13 Jan 2023 14:23:35 -0500 Subject: [PATCH] Refactor the xhr so other requests can reuse the code --- crates/lox-wasm/index.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 0d48133..abf80e2 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -7,6 +7,19 @@ init().then(() => { }); 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) => { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { @@ -20,8 +33,7 @@ function request_open_invite() { return; } const response = JSON.parse(xhr.responseText); - console.log("Got invitation token: "+response.invite); - fulfill(response.invite); + fulfill(response); return; }; try {