Is there a way to make this function recursive without using setTimeout? Basically once all the work for the choicesObj[answer.choice]()
is complete, I want to recall the prompt with the choices list. The problem right now is that it will call the prompt after 2 seconds even if the associated function with choicesObj[answer.choice]()
is not done running.
const choicesObj = {
'View all departments': viewDepartments,
'View all roles': viewRoles,
'View all employees': viewEmployees,
'View employees by manager': viewByManager,
'View employees by department': viewByDepartment,
'Add a department': addDepartment,
'Add a role': addRole,
'Add an employee': addEmployee,
'Update an employee role': updateRole,
'Update employee manager': updateManager,
'Delete a department': deleteDepartment,
'Delete a role': deleteRole,
'Delete an employee': deleteEmployee,
'EXIT': exitApp
}
const prompt = async () => {
const answer = await inquirer.prompt({
name: 'choice',
type: 'list',
message: 'What would you like to do?',
choices: Object.keys(choicesObj)
})
if (answer) {
choicesObj[answer.choice]();
setTimeout(function(){
prompt(); // recursion after 2s
}, 2000);
}
};