-1

There is an array of layes:

this.layers = [
      new ThematicLayers(
        this.userRoles,
        this.layersManager,
        this.map
      ),
      new CommonLayers(
        this.userRoles,
        this.layersManager,
        this.map
      ),
    ];

And abstraction:

export abstract class Layer {
  protected layers: Promise<any> = Promise.resolve(null);

  protected layerGroup: LayerGroup;

  protected grouppedNodes: Map<ITreeNode, ITreeNode[]>;

  abstract load(): Promise<LayerGroup>;

  abstract prepareLayers<T>(layers: T[]): T;

  abstract hasPermission(): boolean;

  abstract createLayersGroup(): void;
 
  get group() {
    return this.layerGroup;
  }

  get stateLayers() {
    return this.reonMap.state;
  }

}

I try iterate the this.layers:

  async getLayersGroups(): Promise<LayerGroup[]> {
    return this.layers.map((layer: Layer) => {
      layer.prepareLayers(await layer.load());
      layer.createLayersGroup();
      return layer.group;
    });
  }

Problem is on line:

layer.prepareLayers(await layer.load());

How to use await in map?

1 Answers1

1

The right way would be to add a async in front of the map callback:

 async getLayersGroups(): Promise<LayerGroup[]> {
    return this.layers.map(async(layer: Layer) => {
      layer.prepareLayers(await layer.load());
      layer.createLayersGroup();
      return layer.group;
    });
  }
Mvrocha
  • 393
  • 3
  • 14