根据答案重复 Inquirer.js 提示问题?
Keep repeating the prompter questions with Inquirer.js based on answer?
我正在使用 Inquirer.js 创建一个 CLI's prompter
,它允许 users
到 enter/reply 到某些 input/questions。在最后一个问题中,我想添加一个功能,如果 user
回复 no
到 Are you done?
问题,那么 prompter
将重新开始提问,直到 [=14] =] 回复 yes
。我的功能快到位了。
它在工作,但仅在我第一次输入 no
时有效。第二次输入 no
,提示符停止。
我如何 运行 循环执行此操作以完成所需的行为?我做错了什么?
这是我的一些远:
import inquirer from 'inquirer';
inquirer
.prompt([
// { bunch of other questions previously },
{
type: 'confirm',
name: 'repeat_questions',
message: 'Are you done?',
},
])
.then((answers) => {
if (answers.repeat_questions) {
return inquirer.prompt([
// { bunch of other questions previously },
{
type: 'confirm',
name: 'repeat_questions',
message: 'Are you done?',
},
]);
}
})
.catch((error) => {
if (error.isTtyError) {
throw new Error(`Prompt couldn't be render in current environment`);
}
});
一种方法是递归函数:
import inquirer from "inquirer";
const questions = [
{
type: "number",
name: "children_count",
message: "How many children do you have?",
},
{
type: "input",
name: "first_child_name",
message: "What is the eldest child's name?",
},
{
type: "confirm",
name: "is_finished",
message: "Are you done?",
},
];
function getAnswers() {
return inquirer.prompt(questions).then((answers) => {
if (answers.is_finished) {
return answers;
} else {
return getAnswers();
}
});
}
getAnswers()
.then(console.log)
.catch((error) => {});
变量repeat_questions
没有意义,如果用户说不完成,repeat_questions
也是不。所以我将它重命名为 is_finished
.
我正在使用 Inquirer.js 创建一个 CLI's prompter
,它允许 users
到 enter/reply 到某些 input/questions。在最后一个问题中,我想添加一个功能,如果 user
回复 no
到 Are you done?
问题,那么 prompter
将重新开始提问,直到 [=14] =] 回复 yes
。我的功能快到位了。
它在工作,但仅在我第一次输入 no
时有效。第二次输入 no
,提示符停止。
我如何 运行 循环执行此操作以完成所需的行为?我做错了什么?
这是我的一些远:
import inquirer from 'inquirer';
inquirer
.prompt([
// { bunch of other questions previously },
{
type: 'confirm',
name: 'repeat_questions',
message: 'Are you done?',
},
])
.then((answers) => {
if (answers.repeat_questions) {
return inquirer.prompt([
// { bunch of other questions previously },
{
type: 'confirm',
name: 'repeat_questions',
message: 'Are you done?',
},
]);
}
})
.catch((error) => {
if (error.isTtyError) {
throw new Error(`Prompt couldn't be render in current environment`);
}
});
一种方法是递归函数:
import inquirer from "inquirer";
const questions = [
{
type: "number",
name: "children_count",
message: "How many children do you have?",
},
{
type: "input",
name: "first_child_name",
message: "What is the eldest child's name?",
},
{
type: "confirm",
name: "is_finished",
message: "Are you done?",
},
];
function getAnswers() {
return inquirer.prompt(questions).then((answers) => {
if (answers.is_finished) {
return answers;
} else {
return getAnswers();
}
});
}
getAnswers()
.then(console.log)
.catch((error) => {});
变量repeat_questions
没有意义,如果用户说不完成,repeat_questions
也是不。所以我将它重命名为 is_finished
.