所以,我正在使用npm在js的控制面板上工作。由于缺少更好的术语,我目前不知如何在代码中途进行重置。我正在尝试这样做,所以当您输入命令时,它会返回菜单,而不启动它的新实例。
我到目前为止的代码:
( async function main() {
for (var d in dirs) {
dir("parent", `${dirs[d]}`);
}
//Filler code to only be ran once.
//Wanted Re-entry Point
info(`Please Enter A Command!\nOr Enter 'Help'!`);
var rep = await query(`\nCommand: `);
rep = rep.toLowerCase();
switch(rep) {
case "help":
var help = require("./Commands/Help.js");
help.CmdList();
//Go back to Re-Entry Point
break;
case "Start Server":
//Menu stuff
//Go to Re-Entry
break;
case "Stop Server":
break;
case "Create Server":
break;
case "Delete Server":
break;
default:
warn("Unknown Command!!");
break;
}
})();
问题是,我不希望它在一开始就重新启动,因为我最终要在开始时添加一些东西,我只想运行一次。除了对其进行调制之外,我该如何实现?
答案 0 :(得分:0)
您已经提供了答案:使用命名的IIFE,然后可以在需要时调用该函数:
(function main() {
const stay = "value";
(function reentry() {
const fresh = Math.random();
console.log(stay, fresh);
if(prompt("again?")) reentry();
})();
})();