简体   繁体   中英

Yeoman generator run async best practice

Im creating a yeoman generator which is working ok, now I need to add a list from async (I want it to run in background) call which can take about 2-3 sec to retrieve the data, so I put it inside the initializing as the user will get this question as the third question. (see object below) so the data retrieve process will start to avoid waiting when user hit question 3. basically I want that the data retrieval will done in a background process..

My question are:

  1. Is they async usage handled ok? I mean running async inside the initialize method

What I tried is the following:


export default class AppGenerator extends Generator {
    private listData: ListInstance[];


    async initializing() {
        this.props = {
            appName: "app",
            apiName: "app-api",
        };
        //--------------Here I call to async function --------------//
        this.listData = await GetInstances();
    }

    async prompting() {
        const answers = await this.prompt([
            {
                name: "appName",
                message: "Project name: ",
                type: "input",
                default: this.props.appName,

            },
            {
                name: "apiName",
                message: "API name: ",
                type: "input",
                default: this.props.apiName,
            },
            {
                name: "instanceName",
                type: "list",
                message: "Choose instance",
                choices: this.listData,
            },
        ];
    }

writing() {

//here I dont get the `this.answers` , I need to get the values from the answers

} 

First some observations:

  • Yeoman Uses Inquirer.js 1
  • Inquirer.js has reactive prompting 2
  • finally, I have not tested this code. Please excuse or point out bugs

also, make sure your answers are accessible outside of the Prompting method (ie using the this for the class - used in example)

...
    async prompting() {
        var prompts = new Rx.Subject();
        this.prompt(prompts);
        prompts.next({
            name: "appName",
            message: "Project name: ",
            type: "input",
            default: this.props.appName,
        });
        prompts.next({
            name: "apiName",
            message: "API name: ",
            type: "input",
            default: this.props.apiName,

        });
        this.listData = await GetInstances();
        prompts.next({

            name: "instanceName",
            type: "list",
            message: "Choose instance",
            choices: this.listData,

        });
        prompts.complete();
        this.answers = this.prompt(prompts).ui.process.subscribe(onEachAnswer, onError, onComplete);
    }
...

Other notes: The use of await will start the call to GetInstances when the Prompting section is started. However, it will not allow the final question to be asked until GetInstances has returned.


1: Yeoman uses Inquirer.js

2: Reactive Prompting in Inquirer.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM