<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I get the final results from my forEach loop? in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056835#M73048</link>
    <description>&lt;P&gt;Glad you figured it out.&amp;nbsp; Just one last thing, the 'catch' for the layer.queryFeatures should be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;.catch(function (error) {
    console.error(outError);
    return defLayer.reject(error);
 });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 May 2021 19:29:12 GMT</pubDate>
    <dc:creator>JeffreyWilkerson</dc:creator>
    <dc:date>2021-05-11T19:29:12Z</dc:date>
    <item>
      <title>How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056366#M73025</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I could use some help (be kind I'm extremely new to JavaScript).&lt;/P&gt;&lt;P&gt;I'm trying to get an array of unique values (MICROFILMS) from all layers/selected features in my map.&lt;/P&gt;&lt;P&gt;My problem is the end result in the program below ends up being an empty array, but logging the array inside the forEach I see my results being built. How do I get the final array after it loops through each layer/feature?&lt;/P&gt;&lt;P&gt;What I think is happening is that my function has not finished adding the values to my array before it moves on. I'm assuming I need to add some sort wait, but I really can't seem to get the logic/syntax down.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /&amp;gt;
    &amp;lt;title&amp;gt;Intro to MapView - Create a 2D map | Sample | ArcGIS API for JavaScript 4.19&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
      #layerToggle {
        top: 20px;
        right: 20px;
        position: absolute;
        z-index: 99;
        background-color: white;
        border-radius: 8px;
        padding: 10px;
        opacity: 0.75;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.19/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require([
          "esri/Map", 
          "esri/views/MapView",
          "esri/layers/FeatureLayer",
          "esri/widgets/LayerList",
          "esri/widgets/Sketch/SketchViewModel",
          "esri/layers/GraphicsLayer",
          "esri/geometry/geometryEngineAsync"
        ], (
            Map, 
            MapView,
            FeatureLayer,
            LayerList,
            SketchViewModel,
            GraphicsLayer,
            geometryEngineAsync
            ) =&amp;gt; {
        const map = new Map({
          basemap: "topo-vector"
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 12,
          center: [-122.228558,47.303845]
        });


//Add Layer
        let stormPipes = new FeatureLayer({
          url: "https://services9.arcgis.com/jyf59MjuiWfY46oy/arcgis/rest/services/Storm_Pipes/FeatureServer",
          id: "Storm Pipes",
          title: "Storm Pipes"
        });
        map.add(stormPipes);
//Add Layer
let stormCatchBasins = new FeatureLayer({
          url: "https://services9.arcgis.com/jyf59MjuiWfY46oy/arcgis/rest/services/Storm_Catch_Basins/FeatureServer",
          id: "Storm Catch Basins",
          title: "Storm Catch Basins"
        });
        map.add(stormCatchBasins);


let microFilms = [];

//layerList with Legend //
const layerList = new LayerList({
  view: view,
  listItemCreatedFunction: function(event) {
    const item = event.item;
    if (item.layer.type != "group") {
      item.panel = {
        content: "legend",
        open: false
      };
    }
  }
});
view.ui.add(layerList, "top-right");
//

//polgon sketch tool//////////////////////////
polygonGraphicsLayer = new GraphicsLayer({listMode: "hide"});
map.add(polygonGraphicsLayer);
view.ui.add("select-by-polygon", "top-left");
const selectButton = document.getElementById("select-by-polygon");
selectButton.addEventListener("click", function() {
  polygonGraphicsLayer.removeAll();
  microFilms = []
  view.popup.close();
  sketchViewModel.create("rectangle");


});

sketchViewModel = new SketchViewModel({
  view: view,
  layer: polygonGraphicsLayer,
  pointSymbol: {
    type: "simple-fill", 
    color: "yellow",
    style: "solid",
    outline: {
    color: "red",
    width: 1
    }
  }

});

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}

sketchViewModel.on("create", async (event) =&amp;gt; {
if (event.state === "complete") {
    
view.map.layers.map(function(lyr){
    if (lyr.visible === true &amp;amp;&amp;amp; lyr.type != "graphics"){
    console.log(lyr.title);
}
});

const geometries = polygonGraphicsLayer.graphics.map(function(graphic){
    return graphic.geometry
});
const queryGeometry = await geometryEngineAsync.union(geometries.toArray());

function getMicroFilms (){
    view.map.layers.forEach((layer) =&amp;gt; {if (layer.visible === true &amp;amp;&amp;amp; layer.type != "graphics"){
    const query = {
    geometry: queryGeometry,
    outFields: ["*"]
};

    layer.queryFeatures(query).then((results) =&amp;gt; {
        results.features.forEach((feature) =&amp;gt; {if (feature.attributes.MICROFILM != "UNKNOWN"){
            microFilms.push(feature.attributes.MICROFILM)}});
            
            console.log(microFilms.filter(onlyUnique))
    
    })
return microFilms
}});
}

console.log(getMicroFilms()); //NEED HELP HERE (this returns an empty array)

   
   

}
});




      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div
    id="select-by-polygon"
    class="esri-widget esri-widget--button esri-widget esri-interactive"
    title="Select features by polygon"
  &amp;gt;    &amp;lt;span class="esri-icon-checkbox-unchecked"&amp;gt;&amp;lt;/span&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 18:54:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056366#M73025</guid>
      <dc:creator>dcarson1661</dc:creator>
      <dc:date>2021-05-10T18:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056420#M73027</link>
      <description>&lt;P&gt;You could probably benefit from cleaning up your indentation to keep track of your scopes. It looks like the return statement for&amp;nbsp;getMicroFilms() is inside the&amp;nbsp;forEach() loop; meaning it will kick out after checking the first feature in the FeatureSet. This is probably what you want the function to look like.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function getMicroFilms () {
    view.map.layers.forEach((layer) =&amp;gt; {
        if (layer.visible === true &amp;amp;&amp;amp; layer.type != "graphics") {
            const query = {
                geometry: queryGeometry,
                outFields: ["*"]
            };
            layer.queryFeatures(query).then((results) =&amp;gt; {
                results.features.forEach((feature) =&amp;gt; {
                    if (feature.attributes.MICROFILM != "UNKNOWN") {
                        microFilms.push(feature.attributes.MICROFILM);
                    }
                });
                console.log(microFilms.filter(onlyUnique));
            });
        }
    });
    return microFilms;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 20:23:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056420#M73027</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-05-10T20:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056424#M73028</link>
      <description>&lt;P&gt;Thanks for the response! That does make things a bit more clear, but I'm still getting an empty array.&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 20:26:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056424#M73028</guid>
      <dc:creator>dcarson1661</dc:creator>
      <dc:date>2021-05-10T20:26:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056457#M73031</link>
      <description>&lt;P&gt;Try logging the query results before you start looping so you can verify if there are actually any features where&amp;nbsp;MICROFILM != "UNKNOWN"&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 21:54:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056457#M73031</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-05-10T21:54:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056478#M73032</link>
      <description>&lt;P&gt;The layers forEach will cycle through each layer synchronously, but the call to 'queryFeatures' results in an asynchronous promise.&amp;nbsp; Esri has a PromiseAll option that I haven't been able to figure out yet, but I have good results using the Dojo versions.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you add&amp;nbsp;"dojo/Deferred", "dojo/promise/all" to the 'require' statement (and the associated variables), you might try something like this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;let promises = [];
let microFilms = [];
function getMicroFilms () {
    view.map.layers.forEach((layer) =&amp;gt; {
        if (layer.visible === true &amp;amp;&amp;amp; layer.type != "graphics") {
            const query = {
                geometry: queryGeometry,
                outFields: ["*"]
            };
            let defLayer = new Deferred;
            promises.push(defLayer);
            layer.queryFeatures(query)
                .then((results) =&amp;gt; {
                    results.features.forEach((feature) =&amp;gt; {
                        if (feature.attributes.MICROFILM != "UNKNOWN") {
                            microFilms.push(feature.attributes.MICROFILM);
                        }
                    });
                    return defLayer.resolve(results);
                })
                .catch(function (error) {
                    console.error(outError);
                    return defAmenities.reject(error);
                });
        }
    });

    all(promises) {
        console.log(microFilms.filter(onlyUnique));
        return microFilms;
    }
}&lt;/LI-CODE&gt;&lt;P&gt;That may work.&amp;nbsp; If not, separate the queryFeatures calls into another function and create the deferred there.&amp;nbsp; Either way the All will only run after all of the separate deferreds have run successfully or failed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 May 2021 22:50:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056478#M73032</guid>
      <dc:creator>JeffreyWilkerson</dc:creator>
      <dc:date>2021-05-10T22:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056506#M73035</link>
      <description>&lt;P&gt;Thank you, I think this might work but I could use some help implementing. I've updated the requirements and function but am getting an error at line: all(promises) {&lt;/P&gt;&lt;P&gt;Error: ';' expected&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /&amp;gt;
    &amp;lt;title&amp;gt;Intro to MapView - Create a 2D map | Sample | ArcGIS API for JavaScript 4.19&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
      #layerToggle {
        top: 20px;
        right: 20px;
        position: absolute;
        z-index: 99;
        background-color: white;
        border-radius: 8px;
        padding: 10px;
        opacity: 0.75;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.19/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require([
          "esri/Map", 
          "esri/views/MapView",
          "esri/layers/FeatureLayer",
          "esri/widgets/LayerList",
          "esri/widgets/Sketch/SketchViewModel",
          "esri/layers/GraphicsLayer",
          "esri/geometry/geometryEngineAsync",
          "esri/layers/GroupLayer",
          "dojo/Deferred",
          "dojo/promise/all"
        ], (
            Map, 
            MapView,
            FeatureLayer,
            LayerList,
            SketchViewModel,
            GraphicsLayer,
            geometryEngineAsync,
            GroupLayer,
            Deferred,
            all
            ) =&amp;gt; {
        const map = new Map({
          basemap: "topo-vector"
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 12,
          center: [-122.228558,47.303845]
        });


//Add Layer
        let stormPipes = new FeatureLayer({
          url: "https://services9.arcgis.com/jyf59MjuiWfY46oy/arcgis/rest/services/Storm_Pipes/FeatureServer",
          id: "Storm Pipes",
          title: "Storm Pipes"
        });
        map.add(stormPipes);
//Add Layer
let stormCatchBasins = new FeatureLayer({
          url: "https://services9.arcgis.com/jyf59MjuiWfY46oy/arcgis/rest/services/Storm_Catch_Basins/FeatureServer",
          id: "Storm Catch Basins",
          title: "Storm Catch Basins"
        });
        map.add(stormCatchBasins);


let microFilms = [];

//layerList with Legend //
const layerList = new LayerList({
  view: view,
  listItemCreatedFunction: function(event) {
    const item = event.item;
    if (item.layer.type != "group") {
      item.panel = {
        content: "legend",
        open: false
      };
    }
  }
});
view.ui.add(layerList, "top-right");
//

/* var stormGroup = new GroupLayer({
        title: "Storm",
        layers: [stormPipes, stormCatchBasins]
      });

      map.add(stormGroup); 
console.log(stormGroup)
*/


//polgon sketch tool//////////////////////////
polygonGraphicsLayer = new GraphicsLayer({listMode: "hide"});
map.add(polygonGraphicsLayer);
view.ui.add("select-by-polygon", "top-left");
const selectButton = document.getElementById("select-by-polygon");
selectButton.addEventListener("click", function() {
  polygonGraphicsLayer.removeAll();
  microFilms = []
  view.popup.close();
  sketchViewModel.create("rectangle");


});

sketchViewModel = new SketchViewModel({
  view: view,
  layer: polygonGraphicsLayer,
  pointSymbol: {
    type: "simple-fill", 
    color: "yellow",
    style: "solid",
    outline: {
    color: "red",
    width: 1
    }
  }

});

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}

sketchViewModel.on("create", async (event) =&amp;gt; {
if (event.state === "complete") {
    
view.map.layers.map(function(lyr){
    if (lyr.visible === true &amp;amp;&amp;amp; lyr.type != "graphics" &amp;amp;&amp;amp; lyr.type != "group"){
    console.log(lyr.title);
}
});

const geometries = polygonGraphicsLayer.graphics.map(function(graphic){
    return graphic.geometry
});
const queryGeometry = await geometryEngineAsync.union(geometries.toArray());

let promises = [];
let microFilms = [];
function getMicroFilms () {
    view.map.layers.forEach((layer) =&amp;gt; {
        if (layer.visible === true &amp;amp;&amp;amp; layer.type != "graphics") {
            const query = {
                geometry: queryGeometry,
                outFields: ["*"]
            };
            let defLayer = new Deferred;
            promises.push(defLayer);
            layer.queryFeatures(query)
                .then((results) =&amp;gt; {
                    results.features.forEach((feature) =&amp;gt; {
                        if (feature.attributes.MICROFILM != "UNKNOWN") {
                            microFilms.push(feature.attributes.MICROFILM);
                        }
                    });
                    return defLayer.resolve(results);
                })
                .catch(function (error) {
                    console.error(outError);
                    return defAmenities.reject(error);
                });
        }
    });

    all(promises) {
        console.log(microFilms.filter(onlyUnique));
        return microFilms;
    }
}

console.log(getMicroFilms());
    
}});




      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div
    id="select-by-polygon"
    class="esri-widget esri-widget--button esri-widget esri-interactive"
    title="Select features by polygon"
  &amp;gt;    &amp;lt;span class="esri-icon-checkbox-unchecked"&amp;gt;&amp;lt;/span&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 00:39:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056506#M73035</guid>
      <dc:creator>dcarson1661</dc:creator>
      <dc:date>2021-05-11T00:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056729#M73043</link>
      <description>&lt;P&gt;Yeah, sorry, you can't just leave 'All' hanging like that, it's actually another promise so it needs a function for its asynchronous result.&amp;nbsp; I've updated your code below to not error out.&amp;nbsp; I'm not sure how you are trying to access the sketch mode, but at least it won't error out when you run it.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /&amp;gt;
    &amp;lt;title&amp;gt;Intro to MapView - Create a 2D map | Sample | ArcGIS API for JavaScript 4.19&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        html,
        body,
        #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }

        #layerToggle {
            top: 20px;
            right: 20px;
            position: absolute;
            z-index: 99;
            background-color: white;
            border-radius: 8px;
            padding: 10px;
            opacity: 0.75;
        }
    &amp;lt;/style&amp;gt;

    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.19/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
        require([
            "esri/Map",
            "esri/views/MapView",
            "esri/layers/FeatureLayer",
            "esri/widgets/LayerList",
            "esri/widgets/Sketch/SketchViewModel",
            "esri/layers/GraphicsLayer",
            "esri/geometry/geometryEngineAsync",
            "esri/layers/GroupLayer",
            "dojo/Deferred",
            "dojo/promise/all"
        ], (
            Map,
            MapView,
            FeatureLayer,
            LayerList,
            SketchViewModel,
            GraphicsLayer,
            geometryEngineAsync,
            GroupLayer,
            Deferred,
            all
        ) =&amp;gt; {
            const map = new Map({
                basemap: "topo-vector"
            });

            const view = new MapView({
                container: "viewDiv",
                map: map,
                zoom: 12,
                center: [-122.228558, 47.303845]
            });


            //Add Layer
            let stormPipes = new FeatureLayer({
                url: "https://services9.arcgis.com/jyf59MjuiWfY46oy/arcgis/rest/services/Storm_Pipes/FeatureServer",
                id: "Storm Pipes",
                title: "Storm Pipes"
            });
            map.add(stormPipes);
            //Add Layer
            let stormCatchBasins = new FeatureLayer({
                url: "https://services9.arcgis.com/jyf59MjuiWfY46oy/arcgis/rest/services/Storm_Catch_Basins/FeatureServer",
                id: "Storm Catch Basins",
                title: "Storm Catch Basins"
            });
            map.add(stormCatchBasins);


            let microFilms = [];

            //layerList with Legend //
            const layerList = new LayerList({
                view: view,
                listItemCreatedFunction: function (event) {
                    const item = event.item;
                    if (item.layer.type != "group") {
                        item.panel = {
                            content: "legend",
                            open: false
                        };
                    }
                }
            });
            view.ui.add(layerList, "top-right");
            //

            /* var stormGroup = new GroupLayer({
                    title: "Storm",
                    layers: [stormPipes, stormCatchBasins]
                  });

                  map.add(stormGroup);
            console.log(stormGroup)
            */


            //polgon sketch tool//////////////////////////
            polygonGraphicsLayer = new GraphicsLayer({ listMode: "hide" });
            map.add(polygonGraphicsLayer);
            view.ui.add("select-by-polygon", "top-left");
            const selectButton = document.getElementById("select-by-polygon");
            selectButton.addEventListener("click", function () {
                polygonGraphicsLayer.removeAll();
                microFilms = []
                view.popup.close();
                sketchViewModel.create("rectangle");


            });

            sketchViewModel = new SketchViewModel({
                view: view,
                layer: polygonGraphicsLayer,
                pointSymbol: {
                    type: "simple-fill",
                    color: "yellow",
                    style: "solid",
                    outline: {
                        color: "red",
                        width: 1
                    }
                }

            });

            function onlyUnique(value, index, self) {
                return self.indexOf(value) === index;
            }

            sketchViewModel.on("create", async (event) =&amp;gt; {
                if (event.state === "complete") {

                    view.map.layers.map(function (lyr) {
                        if (lyr.visible === true &amp;amp;&amp;amp; lyr.type != "graphics" &amp;amp;&amp;amp; lyr.type != "group") {
                            console.log(lyr.title);
                        }
                    });

                    const geometries = polygonGraphicsLayer.graphics.map(function (graphic) {
                        return graphic.geometry
                    });
                    const queryGeometry = await geometryEngineAsync.union(geometries.toArray());

                    let promises = [];
                    //let microFilms = [];
                    function getMicroFilms() {
                        view.map.layers.forEach((layer) =&amp;gt; {
                            if (layer.visible === true &amp;amp;&amp;amp; layer.type != "graphics") {
                                const query = {
                                    geometry: queryGeometry,
                                    outFields: ["*"]
                                };
                                let defLayer = new Deferred;
                                promises.push(defLayer);
                                layer.queryFeatures(query)
                                    .then((results) =&amp;gt; {
                                        results.features.forEach((feature) =&amp;gt; {
                                            if (feature.attributes.MICROFILM != "UNKNOWN") {
                                                microFilms.push(feature.attributes.MICROFILM);
                                            }
                                        });
                                        return defLayer.resolve(results);
                                    })
                                    .catch(function (error) {
                                        console.error(outError);
                                        return defAmenities.reject(error);
                                    });
                            }
                        });

                        all(promises).then(function (results) {
                            console.log("After all promises are returned");
                            console.log(results);
                            console.log(microFilms); //.filter(onlyUnique));
                            return microFilms;
                        });
                    }

                    console.log(microFilms); //getMicroFilms());

                }
            });




        });
    &amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div id="select-by-polygon"
         class="esri-widget esri-widget--button esri-widget esri-interactive"
         title="Select features by polygon"&amp;gt;    &amp;lt;span class="esri-icon-checkbox-unchecked"&amp;gt;&amp;lt;/span&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 16:00:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056729#M73043</guid>
      <dc:creator>JeffreyWilkerson</dc:creator>
      <dc:date>2021-05-11T16:00:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056827#M73047</link>
      <description>&lt;P&gt;You're awesome, this is exactly what I needed! Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 18:57:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056827#M73047</guid>
      <dc:creator>dcarson1661</dc:creator>
      <dc:date>2021-05-11T18:57:09Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get the final results from my forEach loop?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056835#M73048</link>
      <description>&lt;P&gt;Glad you figured it out.&amp;nbsp; Just one last thing, the 'catch' for the layer.queryFeatures should be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;.catch(function (error) {
    console.error(outError);
    return defLayer.reject(error);
 });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 19:29:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-do-i-get-the-final-results-from-my-foreach/m-p/1056835#M73048</guid>
      <dc:creator>JeffreyWilkerson</dc:creator>
      <dc:date>2021-05-11T19:29:12Z</dc:date>
    </item>
  </channel>
</rss>

