From cd4036172da437f096f8cb9c506d782d7f860289 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 16 Jan 2023 15:28:11 +0100 Subject: [PATCH] async functions test --- js/main.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/js/main.js b/js/main.js index 376dec4..5325589 100644 --- a/js/main.js +++ b/js/main.js @@ -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 += `
`; // 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; } }