Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support prompts from array with async factory function and prev answers access #831

Open
GiladShoham opened this issue Jul 31, 2019 · 0 comments

Comments

@GiladShoham
Copy link

Hi,
It will be awesome if the prompts(array) will support async factories function in the array.
Let's take the following example:

async function fetchRestaurantMenu(restaurantName){
// Do some query to DB or http request (async function)
// Will return an array of strings
}
function buildOrderFromRestaurantQuestion(restaurantName) {
  const menu = fetchRestaurantMenu(restaurantName)
  return {
    type: 'list',
    name: 'orderOption',
    message: 'select a dish',
    choices: menu
  };
}

const answers = await inquirer.prompt([buildOrderFromRestaurantQuestion('myRestaurant]);

It will be even better if during the prompt array I will have access to previous answers.
Then I can do something like this (assume functions from previous exists)

// A function used a previous answer to build an order from restaurant question
function buildOrderFromRestaurantQuestionWrapper(answerPropName) {
  // Access somehow to previousAnswers, there might need to be another function to get it
  // it's just a simplified example.
  const restaurantName = previousAnswers[answerPropName]
  return buildOrderFromRestaurantQuestion(restaurantName)
}
const selectRestaurantQ = {
    type: 'list',
    name: 'restaurantName',
    message: 'select a restaurant',
    choices: ['restaurant1', 'restaurant2', 'restaurant3']
  };

const answers = await inquirer.prompt([selectRestaurantQ, buildOrderFromRestaurantQuestionWrapper('restaurantName]);

I know I can achieve this functionality by explicitly calling inquirer.prompt (already done it and it works).
But this way (using the array form) it will be much more elegant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant