我是Javascript的新手,对于我所看到的,我可能在这里遇到异步代码问题:
我有一个函数在我的主生成器中调用composewith。在该函数中调用composeWith之前,如果需要,我使用yosay()。
我的问题是在任何composeWith之前调用所有yoSay函数。
这是我的代码:writing和_composeWith函数
async writing() {
this.log('Before subGenerator 1 ======================================');
if (this.answers.licence) {
await this._ComposeWith(constants.license);
}
this.log('After subGenerator 1 ======================================');
this.log('Before subGenerator 2 ======================================');
if (this.answers.dockerized) {
await this._ComposeWith(constants.docker, {
options: {
packageManager: this.answers.packageManager,
destinationRoot: this._generateRoot()
}
});
}
this.log('After subGenerator 2 ======================================');
}
async _ComposeWith(configObject, args = undefined) {
if (configObject.displayYeoman) {
this.log('YEOMAN ======================================');
//Have Yeoman asking the user some more questions
this.log(
yosay(
`I will now ask you some questions about ${chalk.red(
configObject.context
)} !`
)
);
}
this.log('Calling composeWith ======================================');
this.composeWith(require.resolve(configObject.generator), args);
}
一切都按预期工作,除了在ComposeWith中打印yosay。
输出示例:
Before subGenerator 1 ====================================== YEOMAN ====================================== _-----_ ╭──────────────────────────╮ | | │ I will now ask you some │ |--(o)--| │ questions about the │ `---------´ │ license ! │ ( _´U`_ ) ╰──────────────────────────╯ /___A___\ / | ~ | __'.___.'__ ´ ` |° ´ Y ` Calling composeWith ====================================== After subGenerator 1 ====================================== Before subGenerator 2 ====================================== YEOMAN ====================================== _-----_ | | ╭──────────────────────────╮ |--(o)--| │ I will now ask you some │ `---------´ │ questions about docker ! │ ( _´U`_ ) ╰──────────────────────────╯ /___A___\ / | ~ | __'.___.'__ ´ ` |° ´ Y ` Calling composeWith ====================================== After subGenerator 2 ====================================== ? What's your name: John Doe ? Your email (optional): [email protected] ? Your website (optional): ? Which license do you want to use? Apache 2.0 ? Which version of node will you run in the container? 8 ? Whick package manager will you use? npm create LICENSE create Dockerfile create .dockerignore
任何有关我的错误的帮助/解释将不胜感激! :)
我认为你遇到了run loop的问题。
Yeoman按特定顺序安排某些任务,这适用于组合生成器。我推荐reading the documentation并看看这是否有帮助。
如果有一个发电机,顺序运行任务就没问题。但是,一旦你开始组合发电机,这还不够。
这就是Yeoman使用run循环的原因。
运行循环是具有优先级支持的队列系统。我们使用Grouped-queue模块来处理运行循环。
优先级在您的代码中定义为特殊原型方法名称。当方法名称与优先级名称相同时,运行循环将方法推入此特殊队列。如果方法名称与优先级不匹配,则将其推入默认组。