0

Here in this particular case, I have an array sections, which contains the object required for inquirer prompt.

For Ex, sections[5] may look like:

console.log(JSON.stringify(sections[5], null, '  ');
/** Structure is as follows:
[
    {
        "name": "firstName",
        "type": "input",
        "message": "Enter your first name"
    },
    {
        "name": "secondName",
        "type": "input",
        "message": "Enter your second name"
    }
]
*/

Now I want to prompt for each of object stored in sections array, how can I do that without that long hardcoded nesting using then.?

For now, it looks something like.

inquirer.prompt(sections[1]).then(function(){
    inquirer.prompt(sections[2]).then(function(){
        inquirer.prompt(sections[3]) // and so on...

I want to simplify this. Thanks.

I don't know why but the question is marked as a duplicate of something which is not even close to this. Please take care of this.

pokemon
  • 710
  • 1
  • 5
  • 10
  • @Quentin, can you tell me how is that a duplicate of that link that you have mentioned -_- . Please have a proper look at the question and then mark it duplicate – pokemon Jul 16 '16 at 22:45
  • It seems to me (not familiar with Inquirer) that this could be done with `Array.prototype.forEach`, i.e. - `sections.forEach(function(section) { /* section operation here */ });`, assuming the operations are all the same for each array element. – jdphenix Jul 16 '16 at 22:57
  • If not, then you can save the result result of each item to at least flatten the structure of your code, i.e. `var prompts = []; sections.forEach(function(section) { prompts.push(Function.apply(section)); }; /* prompts is now an array filled with promises for you to pass different functions to `then()` on. */` – jdphenix Jul 16 '16 at 23:04
  • Duplicate voters - A potential duplicate would actually show how to simply code that uses chained promises, not looping through an array... – jdphenix Jul 16 '16 at 23:06
  • I would suggest you to please have a look at inquirer module. It is used to ask for questions on command line, so I just can't store the answers in any variable as at that time, user will be entering the values via terminal. Similarly `forEach` also can't be used. – pokemon Jul 16 '16 at 23:13

0 Answers0