Yeomanrecursion提示,callback不运行

我对Yeoman很新,但即时通讯尝试实现recursion提示。 受jhipster中的EntityGenerator启发( https://github.com/jhipster/generator-jhipster/tree/master/generators/entity )。

然而似乎有一个地方阻止循环发生的错误。

我的代码如下:

var yeoman = require('yeoman-generator'); var inputFields = []; module.exports = yeoman.Base.extend({ prompting: { askForName: askForName, askForFields: askForFields } }); function askForData() { var prompts = [{ type: 'input', name: 'name', message: 'Name?', default: 'Slim Shady' }]; return this.prompt(prompts).then(function (props) { this.props = props; this.async(); }.bind(this)); } function askForFields() { var cb = this.async(); askForField.call(this, cb); } function askForField(cb) { var prompts = [{ type: 'confirm', name: 'fieldAdd', message: 'Do you want to add a field?', default: true }, { when: function (response) { return response.fieldAdd === true; }, type: 'input', name: 'fieldName', message: 'What is the name of your field?' }; this.prompt(prompts, function (props) { this.log("Done prompting: ", props); if (props.fieldAdd) { var field = { fieldName: props.fieldName, }; inputFields.push(field); } if (props.fieldAdd) { askForField.call(that, cb); } else { cb(); } }.bind(this)); } 

recursion提示符的第一个循环正如预期的那样工作,但loggingthis.log("Done prompting: ", props); 从不执行。 它就像提示的callback从来没有发生过。 发电机一次运行后就退出了。

我已经logging并比较了所有function的this ,它们是相同的。 我看了很多类似的stackoverflow问题,但我不能看到任何错误,除了它不工作。

任何帮助或提示表示赞赏!

它看起来像你使用yeoman发电机v0.23.0及以上。

根据yeoman-generator v0.23.0( https://github.com/yeoman/generator/releases/tag/v0.23.0 )的发布:

Base#prompt()函数现在返回一个promise,而不是接受一个callback参数。 这将更容易在asynchronous任务中使用。

 prompting: function () { return this.prompt(questions).then(function (answers) { this.answers = answers; }.bind(this)); } 

这是由于Inquirer.js v1.0.0( https://github.com/SBoudrias/Inquirer.js/releases/tag/v1.0.0 )的发布:

整个查询API现在基于承诺!

  • 基API接口现在是inquirer.prompt(questions).then()。 没有更多的callback函数。

  • 任何asynchronous问题函数都承诺作为返回值而不是要求this.async()。


进入你的askForField()函数,你必须replace:

 this.prompt(prompts, function (props) { this.log("Done prompting: ", props); if (props.fieldAdd) { var field = { fieldName: props.fieldName, }; inputFields.push(field); } if (props.fieldAdd) { askForField.call(that, cb); } else { cb(); } }.bind(this)); 

通过:

 return this.prompt(prompts).then(function (props) { this.log("Done prompting: ", props); if (props.fieldAdd) { var field = { fieldName: props.fieldName, }; inputFields.push(field); } if (props.fieldAdd) { askForField.call(that, cb); } else { cb(); } }.bind(this)); 

正如你在askForData()函数中所做的那样:

 return this.prompt(prompts).then(function (props) { this.props = props; this.async(); }.bind(this));