0

我如何能够从上一个问题中得到答案并传递到下一个问题以供inquirerJS 选择?我知道它是异步的,但我不确定如何在这里创建承诺。任何进一步的见解和帮助都会很棒。

例子:

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
   };
4

1 回答 1

1

根据文档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);
});
于 2018-02-06T09:39:26.770 回答