我如何一次使用多个询问者进行JavaScript

发布于 2025-02-04 18:20:16 字数 1524 浏览 2 评论 0原文

inquirer 
    .prompt([
        {
            name: 'name',
            message: 'Enter team members name.',
        },
        {
            name: 'role',
            type: 'list',
            message: 'Enter a team members role',
            choices: [
                'Engineer',
                'Manager',
                'Intern'
            ],
        },
        {
            name: 'id',
            message: 'Please enter team members id',
            type: 'input'
        },
        {
            name: 'email',
            message: 'Please enter team members email'
        }

    ])
    .then(function(data){
        let moreRole = '';

        if (data.role === 'Engineer') {
            moreRole = 'Github username.'
        }else if(data.role === 'Intern') {
            moreRole = 'school name.'
        }else {
            moreRole = 'office Number.'
        }        
    })


    inquirer
        .prompt([
            {
                message: `Enter team members ${moreRole}`,
                name: 'moreRole'
            },
            {
                type: 'list',
                message: 'Would you like to add another team member?',
                choices: [
                    'Yes',
                    'No'
                ],
                name: 'anotherMember'
            }
        ])
        .then(moredata => {
            console.log(moredata)
        });

因此,第一个询问者可以工作,但是当我添加第二个调查器并运行它时。它无法正常工作。我不能使用两个询问者背对背吗?我将如何工作。在控制台中,它只是问我第一个问题,然后回去,不允许我回答。

inquirer 
    .prompt([
        {
            name: 'name',
            message: 'Enter team members name.',
        },
        {
            name: 'role',
            type: 'list',
            message: 'Enter a team members role',
            choices: [
                'Engineer',
                'Manager',
                'Intern'
            ],
        },
        {
            name: 'id',
            message: 'Please enter team members id',
            type: 'input'
        },
        {
            name: 'email',
            message: 'Please enter team members email'
        }

    ])
    .then(function(data){
        let moreRole = '';

        if (data.role === 'Engineer') {
            moreRole = 'Github username.'
        }else if(data.role === 'Intern') {
            moreRole = 'school name.'
        }else {
            moreRole = 'office Number.'
        }        
    })


    inquirer
        .prompt([
            {
                message: `Enter team members ${moreRole}`,
                name: 'moreRole'
            },
            {
                type: 'list',
                message: 'Would you like to add another team member?',
                choices: [
                    'Yes',
                    'No'
                ],
                name: 'anotherMember'
            }
        ])
        .then(moredata => {
            console.log(moredata)
        });

So the first inquirer works but when I add the second one and run it. It doesn't work properly. Can I not use two inquirers back to back? How would I get it to work. In the console it just asks me the first question and and goes back out and doesn't let me answer it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2025-02-11 18:20:16

包装的API基于承诺。第一个询问器.prompt呼叫启动第一个提示,但是由于您使用的是然后,然后处理已解决的承诺的方法,因此它不在等待操作完成通向第二个询问器。

要绕过这一点,您可以将代码包装到async函数和预处等待到每个询问器。最后一个)。

应该看起来像这样的东西:

async function callInquirers() {
    const inq1 = await inquirer.prompt([...]);
    const inq2 = await inquirer.prompt([...]);

    // do stuff with results inq1 and inq2
}

如果您想对此事有更多见解,请检查文档是否承诺,事件循环,异步/等待。

The API of the package is based on Promises. The first inquirer.prompt call fires the first prompt, but since you are using the then way of handling resolved Promises, it is not waiting for the operation to complete, it just goes through to the second inquirer.prompt call.

To bypass this, you could wrap your code into an async function and prepend await to each inquirer.prompt (or at least to all but the last one).

Should look something like this:

async function callInquirers() {
    const inq1 = await inquirer.prompt([...]);
    const inq2 = await inquirer.prompt([...]);

    // do stuff with results inq1 and inq2
}

Check docs for Promises, event loop, async/await if you want more insight on this matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文