【问题标题】:Inquirer.prompt exiting without an answerInquirer.prompt 退出而没有答案
【发布时间】:2020-11-01 17:27:27
【问题描述】:

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

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();
}

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

【问题讨论】:

  • 对我来说很好。
  • @DaemonBeast 不等待就无法工作

标签: javascript node.js command-line-interface inquirer inquirerjs


【解决方案1】:

您应该使用awaitloadPrompts() 同步工作;

(async function init(){
  await loadPrompts();
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多