javascript - 如何在 Yeoman 中使用 promises 进行递归提示?

标签 javascript recursion yeoman yeoman-generator

我正在尝试弄清楚如何使用 promises 使用 yeoman 生成器执行递归提示。我正在尝试生成一个表单生成器,它首先会为表单组件请求一个名称,然后为每个输入(即:名字、姓氏、用户名等)请求一个名称(将用作 id)。我已经使用回调找到了这个问题的答案,但我想坚持 promise 。下面是我到目前为止的代码以及我试图为递归做的但没有工作。感谢您提供任何帮助和建议,在此先感谢您!

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)
    })
  }

}

最佳答案

对于偶然发现这篇文章以寻找答案的任何人,这就是我最终为使它起作用而做的事情。正如您在我的扩展 Generator 的 Form 类中看到的那样,我在那里有一个名为 prompting() 的方法。这是一个被 Yeoman 的循环识别为优先级的方法,并且在返回某些东西之前它不会离开这个方法。因为我要返回一个 promise ,所以它会等到我的 promise 完成后再继续。对于我的第一个提示,这正是我所需要的,但是对于在 prompting2 中发生的第二个提示,您可以添加

const done = this.async()

在你的方法的开始。这告诉 yeoman 你将要执行一些异步代码,并且在执行 done 之前不要越过包含它的方法。如果您不使用它并且在您的类中有另一个优先方法在它之后,例如 writing() 用于当您准备好生成生成的代码时,那么 yeoman 将跳过您的方法而不等待您的异步代码完成。您可以在我的方法 prompting2() 中看到,只要用户声明有另一个 name 输入,我就会递归调用它,并且它将继续这样做,直到他们说没有另一个 name 输入。我确信有更好的方法可以做到这一点,但这种方式对我来说效果很好。我希望这可以帮助任何正在寻找方法的人!

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

关于javascript - 如何在 Yeoman 中使用 promises 进行递归提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44328745/

相关文章:

javascript - Angular 构建无法在服务器上运行

javascript - 读取 CSV 文件的最后一行并提取一行

c++ - 概率计算器中的段错误

C++ 递归排序算法不需要数组长度

recursion - 在递归过程中显示到输出端口 - 方案

javascript - “Caller”, 'Callee',使用 Yeoman 部署时 Angular 应用程序出现严格模式错误

node.js - Yeoman 和 ExpressJS

Javascript - 更新循环外的值,然后使用新值

javascript - 非字符串变量可以进入 window.onerror 事件的处理程序吗?

javascript - Vanilla WebComponents 接近 : is there any real difference between "importing js from html" and "fetching html from js" file