AnsweredAssumed Answered

conversion of mutiple IdentifyTasks from DeferredList to Promise and All

Question asked by schlot on Oct 2, 2013
Latest reply on Oct 7, 2013 by schlot
I have been using this function on a map click event to run multiple identify tasks using deferred and deferredList.  I'm now hoping to get this rewritten to use dojo/promise/all.  I'm getting lost on what should be changed in this code.

Here is the original, and it works.
function runIdentifies(evt) { idPoint = evt.mapPoint;     var layers = dojo.map(map.layerIds, function(layerId) {         return map.getLayer(layerId);     }); //Create an array of all layers in the map     layers = dojo.filter(layers, function(layer) {         if (layer.visibleLayers[0] !== -1){         return layer.getImageUrl && layer.visible         }     }); //Only dynamic layers have the getImageUrl function. Filter so you only query visible dynamic layers     var tasks = dojo.map(layers, function(layer) {         return new esri.tasks.IdentifyTask(layer.url);     }); //map each visible dynamic layer to a new identify task, using the layer url     var defTasks = dojo.map(tasks, function (task) {         return new dojo.Deferred();     }); //map each identify task to a new dojo.Deferred     var params = createIdentifyParams(layers,evt);     var dlTasks = new dojo.DeferredList(defTasks); //And use all of these Deferreds in a DeferredList     dlTasks.then(showIdentifyResults); //chain showIdentifyResults onto your DeferredList     for (i=0;i<tasks.length;i++) { //Use 'for' instead of 'for...in' so you can sync tasks with defTasks         try {             tasks[i].execute(params[i], defTasks[i].callback, defTasks[i].errback); //Execute each task         } catch (e) {             console.log("Error caught");             console.log(e);             defTasks[i].errback(e); //If you get an error for any task, execute the errback         }     } } //needed to limit identify to only currently visible layers function createIdentifyParams(layers,evt){ identifyParamsList.length = 0; dojo.forEach(layers, function (layer) {     var idParams = new esri.tasks.IdentifyParameters();     idParams.width = map.width;     idParams.height = map.height;     idParams.geometry = evt.mapPoint;     idParams.mapExtent = map.extent;     idParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE;     var visLayers = layer.visibleLayers;     if (visLayers !== -1) {     idParams.layerIds = layer.visibleLayers;     }else {     idParams.layerIds = [];     }     idParams.tolerance = 3;     idParams.returnGeometry = true;     idParams.spatialReference = spatialReference;     identifyParamsList.push(idParams);     });     return identifyParamsList; }  function showIdentifyResults(r) {     var results = [];     r = dojo.filter(r, function (result) {         return r[0];     }); //filter out any failed tasks     for (i=0;i<r.length;i++) {         results = results.concat(r[i][1]);     }      results = dojo.map(results, function(result) {         var feature = result.feature;          var layerName = result.layerName;         feature.attributes.layerName = result.layerName;          feature.setInfoTemplate(generateInfoTemplate);         var resultGeometry = feature.geometry;         var resultType = resultGeometry.type;             if (layerName == "House" || layerName == "Congressional" ) {             feature.attributes.nameTitle = "Representative ";             feature.setInfoTemplate(legisInfoTemplate);         }                              if (layerName == "Senate") {             feature.attributes.nameTitle = "Senator ";             feature.setInfoTemplate(legisInfoTemplate);         }           return feature;              });             if(results.length === 0) {                 map.infoWindow.clearFeatures();             } else {                 map.infoWindow.setFeatures(results);             }             map.infoWindow.show(idPoint);             return results; }   

Here's what I have so far, trying to substitute with Deferred and all.  Or should it be Promise and all?  From my reading, all and deferredList are very similar, but not so for Deferred vs. Promise
function createIdentifyParams(layers,evt){ identifyParamsList.length = 0; arrayUtils.forEach(layers, function (layer) {     var idParams = new IdentifyParameters();     idParams.width = app.map.width;     idParams.height = app.map.height;     idParams.geometry = evt.mapPoint;     idParams.mapExtent = app.map.extent;     idParams.layerOption = IdentifyParameters.LAYER_OPTION_VISIBLE;     var visLayers = layer.visibleLayers;     if (visLayers !== -1) {     idParams.layerIds = layer.visibleLayers;     }else {     idParams.layerIds = [];     }     idParams.tolerance = 3;     idParams.returnGeometry = true;     idParams.spatialReference = spatialReference;     identifyParamsList.push(idParams);     });     return identifyParamsList; }  function runIdentifies(evt) { idPoint = evt.mapPoint; //Create an array of all layers in the map     var layers = arrayUtils.map(app.map.layerIds, function(layerId) {         return app.map.getLayer(layerId);     });  //Only dynamic layers have the getImageUrl function. Filter so you only query visible dynamic layers     layers = arrayUtils.filter(layers, function(layer) {         if (layer.visibleLayers[0] !== -1){         return layer.getImageUrl && layer.visible         }     }); //map each visible dynamic layer to a new identify task, using the layer url      var tasks = arrayUtils.map(layers, function(layer) {         return new IdentifyTask(layer.url);     });      var defTasks = arrayUtils.map(tasks, function (task) {       //  return new dojo.Deferred();  // original code       return new Deferred();     }); //map each identify task to a new dojo.Deferred //function to create an array of parameters, limits identify to only visible layers     var params = createIdentifyParams(layers,evt);     var dlTasks = new all(defTasks);     // var dlTasks = new dojo.DeferredList(defTasks); //And use all of these Deferreds in a DeferredList, original code     dlTasks.then(showIdentifyResults);      for (i=0;i<tasks.length;i++) {          try {             tasks[i].execute(params[i], defTasks[i].callback, defTasks[i].errback);          } catch (e) {             console.log("Error caught");             console.log(e);             defTasks[i].errback(e);          }     } }  function showIdentifyResults(r) {     var results = [];     r = dojo.filter(r, function (result) {         return r[0];     }); //filter out any failed tasks     for (i=0;i<r.length;i++) {         results = results.concat(r[i][1]);     }      results = arrayUtils.map(results, function(result) {         var feature = result.feature;          var layerName = result.layerName;         feature.attributes.layerName = result.layerName;          feature.setInfoTemplate(generateInfoTemplate);         var resultGeometry = feature.geometry;         var resultType = resultGeometry.type;             if (layerName == "House" || layerName == "Congressional" ) {             feature.attributes.nameTitle = "Representative ";             feature.setInfoTemplate(legisInfoTemplate);         }                              if (layerName == "Senate") {             feature.attributes.nameTitle = "Senator ";             feature.setInfoTemplate(legisInfoTemplate);         }           return feature;              });             if(results.length === 0) {                 map.infoWindow.clearFeatures();             } else {                 map.infoWindow.setFeatures(results);             }             map.infoWindow.show(idPoint);             return results; }   

Outcomes