2017-06-02 22 views
3

我想弄清楚如何使用承諾做一個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) 
    }) 
    } 

} 

回答

1

對於任何在這篇文章中找不到答案的人尋找答案,這是我最終做的工作。正如你可以在我的Form類中看到的那樣擴展Generator,我在那裏有一個叫做prompting()的方法。這是Yeoman循環認可的一種優先方法,直到返回某個東西后纔會離開該方法。由於我要回復承諾,所以在繼續之前,我會等到承諾完成。對於我的第一個提示,這正是我需要的,但第二個在提示2中發生,您可以添加

const done = this.async() 

在您的方法開始。這會告訴yeoman你將要發生一些異步代碼,並且不會移過包含這個的方法直到執行完成。如果你不使用它,並且在你的類中有另一個優先級方法,例如在你準備產生你生成的代碼時編寫(),那麼yeoman將移動你的方法而不用等待你的異步代碼完成。你可以在我的方法prompting2()中看到我遞歸地調用它,只要用戶聲明有另一個名稱輸入,它會繼續這樣做,直到他們說沒有其他名稱輸入爲止。我確信有更好的方法可以做到這一點,但這對我來說非常有用。我希望這可以幫助任何正在尋找方法的人!

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 
+0

'this.async'不是絕對必要的。相反,您可以將'return this.prompting2()'返回給chain promise解析。這不是尤曼特有的,這就是Promise的工作方式。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise –

+0

謝謝你的澄清!我會編輯我的答案 – pbie42