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:
initialize
methodWhat 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:
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 toGetInstances
when thePrompting
section is started. However, it will not allow the final question to be asked untilGetInstances
has returned.
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.