【发布时间】:2017-06-15 18:26:17
【问题描述】:
我正在使用 Inquirer & Typescript 2.3 作为控制台应用程序,下面是内容
- 我想让用户使用 await 从列表中选择一个名称。没问题
- 之后,我希望用户选择一个或多个他想要调整的选项。没问题
- 我想循环遍历所有选择的选项,使用与以前相同的 async/await。
- 我想填充
answers对象,例如{fieldName: chosenOption, fieldname2: chosenOption2}
但这就是我遇到麻烦的地方。我尝试了各种选项,但无论我使用哪种循环,我最终都会得到所有 * qeuestions 来回答并让它们具有相同的值。
我看到这是一个广泛解释的主题,但我只是忽略了一些显而易见的事情。为了不占用空间,this.ask 类属性作为 gist 包含在内。
async startInteractive() {
let names = Object.keys(this.config('connect'));
let name = await this.ask.list('name', names);
console.log('need to edit ', name);
let availableFields = [ 'user', 'host', 'port', 'method', 'localPath', 'hostPath' ]
let chosenFields: Answers = await this.ask.checkbox('Choose fields to edit', availableFields)
let current = this.config('connect.' + name);
let answers = {}
// THIS PART
// is the issue i'm not able to tackle
await chosenFields.map(async (field) => {
answers[ field ] = await this.ask.ask(field)
})
}
编辑:这是问题的直观表示:
【问题讨论】:
-
避免在您的
InputHelper方法中使用Promiseconstructor antipattern(所有这些方法)!
标签: typescript promise async-await