0

I am new to Javascript, and for what i see, i may have a async code problem here:

I have a function to call composewith in my main generator. Before calling composeWith in that function, I use yosay() if needed.

My problem is that all yoSay function are called before any composeWith.

here's my code: writing and _composeWith functions

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

Everithing works as expected, except the printing of yosay in _ComposeWith.

Output example:

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): john.doe@example.com
? 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

Any help/explaination of my mistake(s) would be much appreciated ! :)

1 Answers1

0

I think you're running into an issue with the run loop.

Yeoman schedule certain tasks in a certain order and this applies through composed generators. I'd recommend reading the documentation and seeing if this help.

Running tasks sequentially is alright if there’s a single generator. But it is not enough once you start composing generators together.

That’s why Yeoman uses a run loop.

The run loop is a queue system with priority support. We use the Grouped-queue module to handle the run loop.

Priorities are defined in your code as special prototype method names. When a method name is the same as a priority name, the run loop pushes the method into this special queue. If the method name doesn’t match a priority, it is pushed in the default group.

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134