我如何才能从上一个问题中获得答案并转到下一个问题以供询问者JS选择?我知道它是异步的,但我不确定如何在这里创建 promise 。任何进一步的见解和帮助都会很棒。
示例:
var questions = [{
name: 'menu',
type: 'checkbox',
message: 'Which food do you like eating?',
choices: ['pizza', 'hotdog', 'hamburger', 'sandwiches'],
}, {
name: 'favorite',
type: 'checkbox',
message: 'Which one do you like most?',
choices: answers.menu, <------- doesn't work because it is asynchronous
};
最佳答案
根据文档choises
可以是一个函数。
var inquirer = require('inquirer');
var questions = [
{
name: 'menu',
type: 'checkbox',
message: 'Which food do you like eating?',
choices: ['pizza', 'hotdog', 'hamburger', 'sandwiches'],
},
{
name: 'favorite',
type: 'checkbox',
message: 'Which one do you like most?',
choices: (answers) => {
return answers.menu;
}
},
];
inquirer.prompt(questions).then(answers => {
console.log(answers);
});
关于javascript - 在 InquirerJS 中传递选择的答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48638144/