0

我想知道是否有人可以帮助我解释为什么下面的代码会导致命令行退出而不等待用户的回答。

init();
function init() {
  loadPrompts();
}
async function loadPrompts() {
  const { choice } = await inquirer.prompt([
    {
      type: "list",
      name: "choice",
      message: "What would you like to do?",
      choices: [
        {
          name: "View All Employees",
          value: "VIEW_EMPLOYEES",
        },
        {
          name: "View All Employees By Department",
          value: "VIEW_EMPLOYEES_BY_DEPARTMENT",
        },
        {
          name: "View All Employees By Manager",
          value: "VIEW_EMPLOYEES_BY_MANAGER",
        },
        {
          name: "Add Employee",
          value: "ADD_EMPLOYEE",
        },
        {
          name: "Remove Employee",
          value: "REMOVE_EMPLOYEE",
        },
        {
          name: "Update Employee Role",
          value: "UPDATE_EMPLOYEE_ROLE",
        },
        {
          name: "Update Employee Manager",
          value: "UPDATE_EMPLOYEE_MANAGER",
        },
        {
          name: "View All Roles",
          value: "VIEW_ROLES",
        },
        {
          name: "Add Role",
          value: "ADD_ROLE",
        },
        {
          name: "Remove Role",
          value: "REMOVE_ROLE",
        },
        {
          name: "View All Departments",
          value: "VIEW_DEPARTMENTS",
        },
        {
          name: "Add Department",
          value: "ADD_DEPARTMENT",
        },
        {
          name: "Remove Department",
          value: "REMOVE_DEPARTMENT",
        },
        {
          name: "Quit",
          value: "QUIT",
        },
      ],
    },
  ]);
  switch (choice) {
    case "VIEW_EMPLOYEES":
      return viewEmployees();
    default:
      return quit();
  }
}
async function viewEmployees() {
  const employees = await db.findAllEmployees();
  console.table(employees);
  loadPrompts();
}

目标是一个简单的命令行应用程序,它要求用户选择一个选项 - 然后根据他们选择的内容执行一个功能。但是正在发生的是应用程序正在运行,显示选项然后立即退出......

4

1 回答 1

1

你应该使用awaitwithloadPrompts()来同步工作;

(async function init(){
  await loadPrompts();
})();
于 2020-07-12T11:07:16.943 回答