Reduce promise based nesting in chat JS func used in desktop, web client

Use async/await to reduce .then() based nesting to improve code
readability
This commit is contained in:
Debanjum Singh Solanky
2023-12-05 00:41:16 -05:00
parent 6e3f66c0f1
commit d124266923
2 changed files with 196 additions and 200 deletions

View File

@@ -346,8 +346,7 @@
chatInput.classList.remove("option-enabled");
// Call specified Khoj API which returns a streamed response of type text/plain
fetch(url, { headers })
.then(response => {
let response = await fetch(url, { headers });
const reader = response.body.getReader();
const decoder = new TextDecoder();
let rawResponse = "";
@@ -454,7 +453,6 @@
});
}
readStream();
});
}
function incrementalChat(event) {

View File

@@ -309,7 +309,7 @@ To get started, just start typing below. You can also type / to see a list of co
return element
}
function chat() {
async function chat() {
// Extract required fields for search from form
let query = document.getElementById("chat-input").value.trim();
let resultsCount = localStorage.getItem("khojResultsCount") || 5;
@@ -351,8 +351,7 @@ To get started, just start typing below. You can also type / to see a list of co
chatInput.classList.remove("option-enabled");
// Call specified Khoj API which returns a streamed response of type text/plain
fetch(url)
.then(response => {
let response = await fetch(url);
const reader = response.body.getReader();
const decoder = new TextDecoder();
let rawResponse = "";
@@ -458,8 +457,7 @@ To get started, just start typing below. You can also type / to see a list of co
});
}
readStream();
});
}
};
function incrementalChat(event) {
if (!event.shiftKey && event.key === 'Enter') {