A deferred problem or something else?

11-03-2018 04:49 PM
Occasional Contributor

I'm not sure what is going on, or why.  I'm sure it has something to do with the deferred results, but don't really understand.  The qryTask.executeForCount is within a for loop and should execute for each key value that it finds.  I am passing in a single query to execute, but it seems to execute for every key for every key.  If there are 4 keys, then is executes 4 times for each key.  I can't get the results until it completes running, then I'm left with 4 results, 4 times.  I would assume that it would run just once each time since I'm giving it one query each time. 

Please help.

for (var key in this.layerList[0].layerObject.layerDefinitions){
   if (this.layerList[0].layerObject.layerDefinitions.hasOwnProperty(key)) {
      var query = new Query();
      query.outFields = ["*"];
      var url = this.layerList[0].layerObject.url + "/" + key;
      query.where = this.layerList[0].layerObject.layerDefinitions[key];
      query.returnGeometry = false;
      query.outSpatialReference = this.map.spatialReference;
      var qryTask = new QueryTask(url);


Tags (2)
0 Kudos
2 Replies
Esri Frequent Contributor

One way to solve this would be to use promise all in order to determine when the query tasks are complete.  Here's an example showing this approach. In this case along with the count of features I also returned the definition expression and sub layer url just for context. 

<!DOCTYPE html>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  <title>Create Map with Custom ArcGISDynamicMapServiceLayer Layer

  <link rel="stylesheet" href="https://js.arcgis.com/3.26/esri/css/esri.css" />
    #mapDiv {
      padding: 0;
      margin: 0;
      height: 100%;


  <script src="https://js.arcgis.com/3.26/"></script>
    var map;

      function (Map, ArcGISDynamicMapServiceLayer, ImageParameters, all, QueryTask, Query) {

        map = new Map("mapDiv", {
          basemap: "streets",
          center: [-98.258, 38.236],
          zoom: 4

        //Use the ImageParameters to set map service layer definitions and map service visible layers before adding to the client map.
        var imageParameters = new ImageParameters();

        //ImageParameters.layerDefinitions takes an array.  The index of the array corresponds to the layer id.
        //In the sample below an element is added in the array at 3, 4, and 5 indexes.
        //Those array elements correspond to the layer id within the remote ArcGISDynamicMapServiceLayer
        var layerDefs = [];

        layerDefs[1] = "STATE_FIPS='41'";
        layerDefs[2] = "STATE_NAME='Oregon'";
        layerDefs[3] = "STATE_NAME='Oregon'";
        imageParameters.layerDefinitions = layerDefs;

        imageParameters.layerIds = [3, 2, 1];
        imageParameters.layerOption = ImageParameters.LAYER_OPTION_SHOW;
        imageParameters.transparent = true;

        //construct ArcGISDynamicMapServiceLayer with imageParameters from above
        var layerUrl =
        var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer(
          layerUrl, {
            "imageParameters": imageParameters

        var query = new Query();
        query.returnGeometry = false;
        var promiseList = [];
        dynamicMapServiceLayer.layerDefinitions.map(function (def, index) {
          query.where = def;
          var queryLayer = layerUrl + "/" + index;
          var task = new QueryTask(queryLayer);
          promiseList.push(task.executeForCount(query).then(function (count) {
            return {
              index: index,
              def: def,
              count: count


        all(promiseList).then(function (results) {
          console.log("All queries are done", results);



  <div id="mapDiv"></div>

0 Kudos
New Contributor III

I'm having the same issue. Did you find a solution?

0 Kudos