I need to create a menu / sub menu that work with nodejs.
I am using a 8 years ago DeejayRaoul’s solution @DeejayRaoul
The problem is the following: In the main menu I select the option “1 = Callback vs Promises” which takes me to a submenu in which I select the option “1 = Test callbacks”, but this option makes an async call, resulting in: that the submenu be displayed again in the console without waiting for the result of the async function to be displayed after thant the submenu options.
Here is my code :
<code>// helpers/util.js
export function sayHi(name) {
console.log("n" + `---> Hi ${name}!` + "n");
}
export function createTitleBox(title, maxLength, caracterToPrint) {
let titleBorder = caracterToPrint.repeat(maxLength);
const sideLength = (maxLength - title.length) / 2;
let newTitle = "";
newTitle = title.padStart(sideLength + title.length, " ");
newTitle = newTitle.padEnd(maxLength, " ");
console.log("n");
console.log(titleBorder + "n");
console.log(newTitle + "n");
console.log(titleBorder + "n");
console.log("n");
}
</code>
<code>// helpers/util.js
export function sayHi(name) {
console.log("n" + `---> Hi ${name}!` + "n");
}
export function createTitleBox(title, maxLength, caracterToPrint) {
let titleBorder = caracterToPrint.repeat(maxLength);
const sideLength = (maxLength - title.length) / 2;
let newTitle = "";
newTitle = title.padStart(sideLength + title.length, " ");
newTitle = newTitle.padEnd(maxLength, " ");
console.log("n");
console.log(titleBorder + "n");
console.log(newTitle + "n");
console.log(titleBorder + "n");
console.log("n");
}
</code>
// helpers/util.js
export function sayHi(name) {
console.log("n" + `---> Hi ${name}!` + "n");
}
export function createTitleBox(title, maxLength, caracterToPrint) {
let titleBorder = caracterToPrint.repeat(maxLength);
const sideLength = (maxLength - title.length) / 2;
let newTitle = "";
newTitle = title.padStart(sideLength + title.length, " ");
newTitle = newTitle.padEnd(maxLength, " ");
console.log("n");
console.log(titleBorder + "n");
console.log(newTitle + "n");
console.log(titleBorder + "n");
console.log("n");
}
<code>// index.js
import { sayHi, createTitleBox } from "./helpers/util.js";
let menuHandler;
// Initialize
function initialize() {
showMain();
process.stdin.setEncoding("utf8");
process.stdin.on("readable", checkMenu);
function checkMenu() {
let input = process.stdin.read();
if (input !== null) {
menuHandler(input.trim());
}
}
}
// Main
function showMain() {
console.log("n" + "x1b[33m Choose a number x1b[0m" + "n");
console.log(
"1 = Callback vs Promises" +
"n" +
"2 = xxxxxxx" +
"n" +
"3 = Exit" +
"nn" +
"Choose a number, then press ENTER:"
);
menuHandler = async function (input) {
switch (input) {
case "1":
show_callback_promises_SubMenu();
break;
case "2":
llama_promise();
break;
case "3":
process.exit();
break;
default:
showMain();
}
};
}
// Sub
function show_callback_promises_SubMenu() {
console.log("n" + "x1b[33m Choose a number x1b[0m" + "n");
console.log(
"1 = Test callbacks" +
"n" +
"2 = Test promesa" +
"n" +
"3 = Go back to main menu" +
"nn" +
"Choose a number, then press ENTER:"
);
menuHandler = async function (input) {
switch (input) {
case "1":
await use_callback();
break;
case "2":
use_promise();
break;
case "3":
showMain();
break;
default:
show_callback_promises_SubMenu();
}
};
}
initialize();
async function use_callback() {
try {
// CALLBACKS
createTitleBox("CALLBACK", 40, "-");
const moduleCallBack = await import("./helpers/callback.js");
await moduleCallBack.default(sayHi);
} catch (err) {
console.log(err);
}
}
async function use_promise() {
try {
// PROMISES
createTitleBox("PROMISES", 40, "-");
const modulePromise = await import("./helpers/promise.js");
modulePromise.default().then((nombre) => sayHi(nombre));
} catch (err) {
console.log(err);
}
}
</code>
<code>// index.js
import { sayHi, createTitleBox } from "./helpers/util.js";
let menuHandler;
// Initialize
function initialize() {
showMain();
process.stdin.setEncoding("utf8");
process.stdin.on("readable", checkMenu);
function checkMenu() {
let input = process.stdin.read();
if (input !== null) {
menuHandler(input.trim());
}
}
}
// Main
function showMain() {
console.log("n" + "x1b[33m Choose a number x1b[0m" + "n");
console.log(
"1 = Callback vs Promises" +
"n" +
"2 = xxxxxxx" +
"n" +
"3 = Exit" +
"nn" +
"Choose a number, then press ENTER:"
);
menuHandler = async function (input) {
switch (input) {
case "1":
show_callback_promises_SubMenu();
break;
case "2":
llama_promise();
break;
case "3":
process.exit();
break;
default:
showMain();
}
};
}
// Sub
function show_callback_promises_SubMenu() {
console.log("n" + "x1b[33m Choose a number x1b[0m" + "n");
console.log(
"1 = Test callbacks" +
"n" +
"2 = Test promesa" +
"n" +
"3 = Go back to main menu" +
"nn" +
"Choose a number, then press ENTER:"
);
menuHandler = async function (input) {
switch (input) {
case "1":
await use_callback();
break;
case "2":
use_promise();
break;
case "3":
showMain();
break;
default:
show_callback_promises_SubMenu();
}
};
}
initialize();
async function use_callback() {
try {
// CALLBACKS
createTitleBox("CALLBACK", 40, "-");
const moduleCallBack = await import("./helpers/callback.js");
await moduleCallBack.default(sayHi);
} catch (err) {
console.log(err);
}
}
async function use_promise() {
try {
// PROMISES
createTitleBox("PROMISES", 40, "-");
const modulePromise = await import("./helpers/promise.js");
modulePromise.default().then((nombre) => sayHi(nombre));
} catch (err) {
console.log(err);
}
}
</code>
// index.js
import { sayHi, createTitleBox } from "./helpers/util.js";
let menuHandler;
// Initialize
function initialize() {
showMain();
process.stdin.setEncoding("utf8");
process.stdin.on("readable", checkMenu);
function checkMenu() {
let input = process.stdin.read();
if (input !== null) {
menuHandler(input.trim());
}
}
}
// Main
function showMain() {
console.log("n" + "x1b[33m Choose a number x1b[0m" + "n");
console.log(
"1 = Callback vs Promises" +
"n" +
"2 = xxxxxxx" +
"n" +
"3 = Exit" +
"nn" +
"Choose a number, then press ENTER:"
);
menuHandler = async function (input) {
switch (input) {
case "1":
show_callback_promises_SubMenu();
break;
case "2":
llama_promise();
break;
case "3":
process.exit();
break;
default:
showMain();
}
};
}
// Sub
function show_callback_promises_SubMenu() {
console.log("n" + "x1b[33m Choose a number x1b[0m" + "n");
console.log(
"1 = Test callbacks" +
"n" +
"2 = Test promesa" +
"n" +
"3 = Go back to main menu" +
"nn" +
"Choose a number, then press ENTER:"
);
menuHandler = async function (input) {
switch (input) {
case "1":
await use_callback();
break;
case "2":
use_promise();
break;
case "3":
showMain();
break;
default:
show_callback_promises_SubMenu();
}
};
}
initialize();
async function use_callback() {
try {
// CALLBACKS
createTitleBox("CALLBACK", 40, "-");
const moduleCallBack = await import("./helpers/callback.js");
await moduleCallBack.default(sayHi);
} catch (err) {
console.log(err);
}
}
async function use_promise() {
try {
// PROMISES
createTitleBox("PROMISES", 40, "-");
const modulePromise = await import("./helpers/promise.js");
modulePromise.default().then((nombre) => sayHi(nombre));
} catch (err) {
console.log(err);
}
}