我想弄清楚如何使用承諾做一個yeoman生成器的遞歸提示。我試圖生成一個表單生成器,它將首先要求表單組件的名稱,然後爲每個輸入(即:firstName,lastName,username等)請求一個名稱(將用作id)。我已經使用回調找到了這個問題的答案,但我想堅持承諾。下面是我到目前爲止的代碼以及我正在嘗試爲遞歸執行的操作,但不起作用。任何幫助和建議,表示感謝提前謝謝!如何用承諾在Yeoman中做遞歸提示?
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)
})
}
}
'this.async'不是絕對必要的。相反,您可以將'return this.prompting2()'返回給chain promise解析。這不是尤曼特有的,這就是Promise的工作方式。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise –
謝謝你的澄清!我會編輯我的答案 – pbie42