title: Run function after all tiles have been processed
body: <p>How do I get a function to run after <a href="https://openlayers.org/en/latest/apidoc/module-ol_tilegrid_TileGrid-TileGrid.html" rel="nofollow noreferrer">forEachTileCoord</a> have been processing all tiles?</p>
  
  <p>I have been writting a script to recolor a WMTS-layer based on the currently displayed tiles. The script is going through all the tiles to figure out what their maximum values are and then color the tiles based the biggest value in any of the displayed tiles. The coloring part works fine, but it doesn't wait for all of the tiles to be processed. </p>
  
  <p>In my code attached at the bottom the last half shouldn't trigger before all tiles have been processed. I presume that I would need to use <a href="https://javascript.info/promise-basics" rel="nofollow noreferrer">Promises</a>. Issue is I don't know how to define when the forEachTileCoord is finished. </p>
  
  <pre><code>  map.on("moveend", function() {
    var tileUrlFunction = tileSource.getTileUrlFunction()
    var maxValues = [];
    var loadExtent = map.getView().calculateExtent(map.getSize());
    //Checks which tiles that currently are being displayed
    tileSource.getTileGrid().forEachTileCoord(loadExtent, mapZoom, function(tileCoord) {
  
      //Gets the name of each currently displayed tile
      tileName = tileUrlFunction(tileCoord, ol.proj.get('EPSG:4326'))
      asyncCall()
  
      async function asyncCall() {
      tileMaxValue = await calculateMaxValue(tileName);
      maxValues.push(tileMaxValue)
      }
  
      })
  
  
  //This next part triggers too early - returns -Infinity
    currentMax = Math.max(...maxValues)
    if (Number.isInteger(currentMax)) {
  //This is the function for recoloring the map
      olgt_map.redraw(olgt_map, currentMax, colorScale);
    }  
    }
  </code></pre>
  
  <p>The redraw function is changing the legend values and the coloring distribution of the layer as illustrated <a href="https://i.stack.imgur.com/ftLI6.png" rel="nofollow noreferrer">here</a>. The two screenshots are two different areas of the same map. Legend and coloring are being adjusted to the max value currently being shown. </p>
  
  <pre><code>olGeoTiff.prototype.redraw = function(map, currentMax, legendValues) {
  
      map.plotOptions.domain = [0, currentMax];
  
    this.layer.getSource().refresh();
    updateLegend(legendValues, currentMax);
  }
  </code></pre>