我正在创建一个Yeoman生成器,它目前运行良好,现在我需要添加一个列表来自异步调用(我希望它在后台运行),该调用可能需要大约2-3秒钟来检索数据,因此我将其放置在initializing
中,因为用户将在第三个问题中得到这个问题。 (请参见下面的对象)因此,数据检索过程将开始以避免等待用户点击第3个问题时。基本上,我希望数据检索将在后台进程中完成。
我的问题是:
- 它的异步使用是否处理正确?我的意思是在
initialize
方法中运行异步代码是否正确。
我尝试了以下内容:
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
}
instanceName
需要访问这些数据。 - Beno Odr