How do I make a recursive query in Yeoman using promises?

I am trying to figure out how to do a recursive hint with a youma generator using promises. I am trying to create a form generator that first asks for a name for a form component and then asks for a name (to be used as an id) for each login (eg: firstName, lastName, username, etc.). I found answers to this question using callbacks, but I would like to stick with promises. Below is the code I have and what I am trying to do for recursion but does not work. Any help and advice is appreciated in advance!

const Generator = require('yeoman-generator')

const questions = [
  { type: 'input',
    name: 'name',
    message: 'What is the name of this form?',
    default: 'someForm'
  },
  {
    type: 'input',
    name: 'input',
    message: 'What is the name of the input?'
  },
  {
    type: 'confirm',
    name: 'askAgain',
    message: 'Is there another input to add?'
  }

]

module.exports = class extends Generator {


  prompting() {
    return this.prompt(questions).then((answers) => {
      if (answers.askAgain) {
        this.prompting()
      }
       this.userOptions = answers
       this.log(this.userOptions)
    })
  }

}

      

+3


source to share


1 answer


For those who stumbled upon this post while looking for an answer, this is what I did to get it to work. As you can see in my Form class that extends the generator, I have a method called prompting (). This is the method recognized by Yeoman's loop as a priority, and it will not leave this method until something is returned. Since I am returning a promise, it will wait until my promise ends before moving on. For my first request, which is exactly what I need, but for the second in prompting2, you can add

const done = this.async()

      



at the beginning of your method. This tells you that you will have some kind of asynchronous code, not to navigate from the method containing this until execution is done. If you do not use this and have another precedence method in your class after it, such as write (), when you are ready to generate the generated code, then yoman will navigate past your method without waiting for your async code to complete. And you can see in my prompting2 () method that I recursively call it whenever the user states that there is another name input, and he will keep doing this until he says there is no other name for the name. I'm sure there is a better way to do it, but it works great for me this way. Hope this helps anyone looking for a way to do this!

const Generator = require('yeoman-generator')

const questions = [
    { type: 'input',
      name: 'name',
      message: 'What is the name of this form?',
      default: 'someForm'
    }
]

const names = [
 {
   type: 'input',
   name: 'inputs',
   message: 'What is the name of the input?',
   default: '.input'
 },
 {
   type: 'confirm',
   name: 'another',
   message: "Is there another input?",
   default: true
 }
]

const inputs = []

class Form extends Generator {

  prompting() {
    return this.prompt(questions).then((answers) => {
      this.formName = answers.name
      this.log(this.formName)
   })
  }

  prompting2 () {
     const done = this.async()
     return this.prompt(names).then((answers) => {
      inputs.push(answers.inputs)
      if (answers.another) this.prompting2()
      else {
       this.inputs = inputs
       this.log(this.inputs)
       done()
      }
    })
  }

}

module.exports = Form

      

+1


source







All Articles