0

我想让 UserDataGenerator 类像传统的 SYNC 类一样工作。

我的期望是userData.outputStructure可以给我准备的数据。

let userData = new UserDataGenerator(dslContent)
userData.outputStructure

getFieldDescribe(this.inputStructure.tableName, field)是一个 ASYNC 调用,它调用Axios.get

以下是我目前的进度,但是当我打印出userData.outputStructure

导出默认类 UserDataGenerator { inputStructure = null; 输出结构=空;fieldDescribeRecords = [];

 constructor(dslContent) {

    this.outputStructure = Object.assign({}, dslContent, initSections)
    process()
}

async process() {
    await this.processSectionList()
    return this.outputStructure
}

async processSectionList() {
    await this.inputStructure.sections.map(section => {
       this.outputStructure.sections.push(this.processSection(section));
    })
}

async processSection(section) {
    let outputSection = {
        name: null,
        fields: []
    }
    let outputFields = await section.fields.map(async(inputField) => {
        return await this._processField(inputField).catch(e => {
            throw new SchemaError(e, this.inputStructure.tableName, inputField)
        })
    })
    outputSection.fields.push(outputFields)
    return outputSection
}

async _processField(field) {
    let resp = await ai
    switch (typeof field) {
        case 'string':
            let normalizedDescribe = getNormalizedFieldDescribe(resp.data)
            return new FieldGenerator(normalizedDescribe, field).outputFieldStructure
    }

}
4

1 回答 1

0

您正在尝试await数组,但它不能按预期工作。在处理 Promise 数组时,您仍然需要先使用Promise.allawait- 就像您不能链接.then数组一样。

所以你的方法应该是这样的:

async processSectionList() {
    const sections = await Promise.all(this.inputStructure.sections.map(section => 
         this.processSection(section)
    ));
    this.outputStructure.sections.push(...sections);
}

async processSection(section) {
    return {
        name: null,
        fields: [await Promise.all(section.fields.map(inputField =>
            this._processField(inputField).catch(e => {
                throw new SchemaError(e, this.inputStructure.tableName, inputField)
            })
        ))]
    };
}
于 2017-04-07T03:01:47.327 回答