code: function correctEntityHeight(
    entity,
    currentCartographicPosition,
    terrainProvider,
    levelHint
  ) {
    sampleTerrain(terrainProvider, levelHint, [currentCartographicPosition]).then(
      function(updatedPositions) {
        if (updatedPositions[0].height !== undefined) {
          entity.position = Ellipsoid.WGS84.cartographicToCartesian(
            updatedPositions[0]
          );
        } else if (levelHint > 0) {
          correctEntityHeight(
            entity,
            currentCartographicPosition,
            terrainProvider,
            levelHint - 1
          );
        }
      }
    );
  }
docs: Gets the most detailed height from terrainProvider at currentCartographicPosition and updates entity position.
  It starts querying at levelHint and makes its way down to level zero.