async functions test

This commit is contained in:
Baipyrus 2023-01-16 15:28:11 +01:00
parent 95fbc3cea4
commit cd4036172d

View File

@ -44,7 +44,7 @@ document.addEventListener("selectionchange", e => {
}
});
document.addEventListener("keydown", e => {
document.addEventListener("keydown", async e => {
const text = textCur.textContent;
switch (e.key) {
case "Backspace":
@ -100,7 +100,7 @@ document.addEventListener("keydown", e => {
tbDiv.innerHTML += `<br>`;
// Run command and return output
let commandOutput = runCommand(text);
let commandOutput = await runCommand(text);
if (commandOutput !== "") {
// If output is given, display it as text with formatting
tbDiv.append(createText(commandOutput, false))
@ -162,7 +162,7 @@ function createText(str, safe=true) {
}
// Run input as command if possible
function runCommand(input) {
async function runCommand(input) {
// Return on no input
if (input === "")
return "";
@ -179,13 +179,19 @@ function runCommand(input) {
const lowerNm = name.toLowerCase();
// If command is called without parameters
if (lowerIn === lowerNm) {
output = window[func]();
if (window[func].constructor.name === "AsnycFunction")
output = await window[func]();
else
output = window[func]();
break;
// If command is called with parameters
} else if (lowerIn.startsWith(lowerNm + " ")) {
// Parameters always follow the command name after first space
const params = input.split(" ").filter((e,i)=>i!==0);
output = window[func](params);
if (window[func].constructor.name === "AsnycFunction")
output = await window[func](params);
else
output = window[func](params);
break;
}
}