How execute other prompt when prompt previous is true on Yeoman?

12,198

Yeoman uses a thing called Inquirer.js for the prompt system. Here's an example of how you can ask Question 2 if Question 1 was true:

inquirer.prompt([{
  name: 'movie',
  type: 'confirm',
  message: 'Have you seen a movie lately?'
}, {
  when: function (response) {
    return response.movie;
  },
  name: 'good-or-not',
  message: 'Sweet! Was it any good?'
}], function (response) {});

From the Inquirer.js documentation:

when: (Function) Receive the current user answers hash and should return true or false depending on wheter or not this question should be asked.

Share:
12,198

Related videos on Youtube

Fuechter
Author by

Fuechter

I'm batman or not.

Updated on May 25, 2022

Comments

  • Fuechter
    Fuechter almost 2 years

    How execute prompt2 when prompt1 is true on Yeoman as shown below?

    var prompts = [
      {name: 'prompt1', message: 'Ask 1?'},
      {name: 'prompt2', message: 'Ask 2?'}
    ];
    
  • Cameron
    Cameron over 7 years
    How would you add on a question if it was true though?
  • Our_Benefactors
    Our_Benefactors about 7 years
    @cameronroe If the user answers 'no' to the question 'Have you seen a movie lately?' then the next message will not display. To add another question after 'Sweet! Was it any good?', add another when: function(response){} with the same syntax. You can also change the type for subsequent questions.
  • Aakash
    Aakash over 5 years
    @Our_Benefactors, the first argument in the when() method is the answers-hash. So if you don't have the required answer for the question, you can return false; in the method function. For example : when: function(ans){ if(ans.prompt1 === false) {return false;} else {return true;}}. Good Luck...
  • Karma Blackshaw
    Karma Blackshaw about 4 years
    Is there a way to pass the params in when to the prompt? I generated a list in the when and I need to pass that list as the choices for the current promt.