【发布时间】:2020-07-29 00:43:20
【问题描述】:
我正在创建一个运行正常的 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
}
【问题讨论】:
-
您能否提供更多详细信息...例如正在触发的 HTML 和事件...我对问题出在哪里有点困惑...应用程序流程...情况如何工作,这个提示功能在做什么?我对你分享的 sn-p 不太了解。
-
@HimanshuBansal - 假设您正在创建和 yeoman 生成器,其中一个问题应该返回来自 rest 调用的数据列表(可能需要 1-3 秒),并且您想要运行它后台处理,当用户点击这个问题时,他不需要等待3秒来获取数据,你会怎么做?
-
我明白了但是..你是在发出任何http请求还是从服务或其他方式..如果它在初始化..它会像你说的那样获取接下来的3个问题..它需要在后台运行..所以我无法理解流程。如果你能清除它..我很乐意以更好的方式帮助你......
-
@HimanshuBansal - 该服务正在调用本地 API(它不公开..)
lets assume its a function that you are calling to some rest which take about 3 seconds (or more, network....) and the data should be fetch at the initialization as we dont want to waste time, but it should be ready on question 3instanceName` 你会怎么做?工作线程?instanceName需要访问这些数据 -
@HimanshuBansal - 现在更清楚了吗?
标签: javascript node.js async-await yeoman yeoman-generator