函数递归建议

Function recursion advice

我正在构建一个员工跟踪应用程序。我希望我的应用程序能够正常工作,以便在完成各种列表选择的每个功能后,该应用程序将再次提示您使用选项列表。我可以想到几种方法来实现这一点,但我希望经验更丰富的开发人员可以推荐哪种方法最好。谢谢!

  1. 将switch语句中的所有函数都做成promise,然后做一个自己调用的promise链
  2. 尝试使用 async-await 而不是 promise 链???
  3. 导出 prompt() 函数并导入它以及每个 lib 文件中的所有其他相应函数

这里是应用程序的入口点

const db = require('./db/connection');
const inquirer = require('inquirer');

// response handling 
const { viewDepartments, viewRoles, viewEmployees, viewByManager, viewByDepartment } = require('./lib/viewFuncs');
const { addDepartment, addRole, addEmployee } = require('./lib/addFuncs');
const { updateRole, updateManager } = require('./lib/updateFuncs');
const { deleteDepartment, deleteRole, deleteEmployee } = require('./lib/deleteFuncs');

function prompt() {
    return inquirer.prompt({
        name: 'choice',
        type: 'list',
        message: 'What would you like to do?',
        choices: [
            'View all departments', 
            'View all roles', 
            'View all employees',
            'View employees by manager',
            'View employees by department', 
            'Add a department', 
            'Add a role',
            'Add an employee', 
            'Update an employee role',
            'Update employee manager',
            'Delete a department',
            'Delete a role',
            'Delete an employee',
            'EXIT'
        ]
    }).then(answer => {
        switch (answer.choice) {
            case 'View all departments':
                viewDepartments();
                break;
            case 'View all roles':
                viewRoles();
                break;
            case 'View all employees':
                viewEmployees();
                break;
            case 'View employees by manager':
                viewByManager();
                break;
            case 'View employees by department':
                viewByDepartment();
                break;
            case 'Add a department':
                addDepartment();
                break;
            case 'Add a role':
                addRole();
                break;
            case 'Add an employee':
                addEmployee();
                break;
            case 'Update an employee role':
                updateRole();
                break;
            case 'Update an employee manager':
                updateManager();
                break;
            case 'Delete a department':
                deleteDepartment();
                break;
            case 'Delete a role':
                deleteRole();
                break;
            case 'Delete an employee':
                deleteEmployee();
                break;
            case 'EXIT': 
                exitApp();
                break;
            default:
                break;
        }
    })
};
// connect to database 
db.connect(err => {
    if (err) throw err;
    console.log('Welcome to the employee-tracker!');
    // start the app 
    prompt();
});
// exit the app
function exitApp() {
    console.log('Thank you for using the employee-tracker!')
    db.end();
};

我认为 switch 语句有点矫枉过正。如果您创建一个对象,其中键是选项,函数是值,那么它是一个 1:1 查找并且假设性能更高。

此外,删除 then 语句,因为我们在这里编写现代代码。也祝贺实习。

const choiceObj = {
            'View all departments':viewDepartments(), 
            'View all roles':viewAllRoles(), 
            ...etc etc 
}

const prompt = async () => {
    const answer = await inquirer.prompt({
        name: 'choice',
        type: 'list',
        message: 'What would you like to do?',
        choices: Object.keys(choiceObj) // returns an array of the object keys
    })

    if (answer) {
       choiceObj[answer]; 

       alert('answer submitted'); // UX++
       setTimeout(function(){
         prompt(); // recursion after 2s
       }, 2000);
    }
};

我可以在评论区回答任何问题。谢谢!