promise.all 解决 map 遍历 异步问题

// 字段初始化
    async fieldInit (fields) {
      // 字段 (isFixed: false 非固定字段)
      let newFields = fields.filter(f => !f.isFixed)
      this.form.customFieldList = await Promise.all(newFields.map((item, index) => this.fieldsFormatter(item, index)))
    },
    fieldsFormatter (item, index) {
      return new Promise(async resolve => {
        if (this.drawerTitle === '新建工单') {
          resolve({
            fieldId: item['id'],
            value: this.fieldValueInit(item),
            fieldName: item['name'],
            fieldType: { type: item['type'], label: this.fieldTypeFormatter(item['type']) },
            required: item['required'],
            optional: await this.getOptional(item['id']),
            _index: index
          })
        } else {
          resolve({
            fieldId: item['fieldId'],
            value: this.fieldValueInit(item),
            fieldName: item['fieldName'],
            fieldType: { type: item['fieldType'], label: this.fieldTypeFormatter(item['fieldType']) },
            required: item['required'],
            optional: item['optional'],
            _index: index
          })
        }
      })
    },
promise.all(forEach).png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容