Yeoman发电机运行异步最佳实践

问题描述 投票:4回答:1

我正在创建可以正常工作的yeoman生成器,现在我需要添加一个列表从异步(我希望它在后台运行)调用,该过程大约需要2-3秒才能检索到数据,因此我将其放在initializing内,因为用户将得到第三个问题。 (请参见下面的对象)因此,当用户遇到问题3时,数据检索过程将开始避免等待。基本上,我希望数据检索将在后台过程中完成。.]]

我的问题是:

  1. 它们是否可以处理异步使用情况?我的意思是在initialize方法中运行异步
  2. 我尝试的是以下内容:


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

} 
    

我正在创建一个可以正常工作的yeoman生成器,现在我需要从异步(我希望它在后台运行)调用中添加一个列表,该列表可能需要2-3秒来检索数据,因此我将其放入...

javascript node.js async-await yeoman yeoman-generator
1个回答
0
投票

[一些初步观察:

© www.soinside.com 2019 - 2024. All rights reserved.