How to tell when all layers and sublayers are done loading

2393
1
01-18-2022 11:32 PM
DavidKlinger
Emerging Contributor

I have a list of both the layers and map-image sublayers, that I want to keep updated.  It should update in the beginning, whenever all the initial layers/sublayers are done loading, as well as whenever a layer is added or removed from the map.  In any case, I need to wait until any new layers and sublayers are done loading, or the list is inaccurate.

This is what I was trying:

 

const layersWatch = mapView.map.allLayers.on('change', (event) => {
  if(event.target) {
    setLayersUpdating(true);
    const allLayerWhens = event.target.map((layer) => {
      return layer.when();
    });

    Promise.allSettled(allLayerWhens).then((loadedLayerResults) => {
      // It gets here with the expected results...
      const allSublayerWhens = [];
      loadedLayerResults.forEach((result) => {
        if (result.value?.allSublayers?.length) {
          result.value.allSublayers.forEach((sublayer) => {
            allSublayerWhens.push(sublayer.when());
          });
        }
      });
     
      Promise.allSettled(allSublayerWhens).then(() => {
        // ...but never gets here:
        setLayersUpdating(false);
      });
    });
  }
});

 

1 Reply