CJL1992
CJL1992

Reputation: 41

Inquirer.prompt exiting without an answer

I was wondering if anybody could help me out with an explanation as to why the below code would cause the command line to exit without waiting for an answer from the user.

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

The aim is a simple command-line application that asks the user to select an option - then depending on what they have selected a function will be executed. But what is happening is that the application is running, showing the options then immediately exiting...

Upvotes: 0

Views: 3614

Answers (1)

Ahmed ElMetwally
Ahmed ElMetwally

Reputation: 2383

You Should use await with loadPrompts() to work synchronously;

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

Upvotes: 2

Related Questions