Multiple services in Identify javascript API

Discussion created by smccle on Jun 1, 2011
Latest reply on Apr 27, 2018 by JeffMay_IDFGgis
I'm trying to implement identify across multiple services (with multiple layers).  I'm having trouble with the asynchronous processing of the identify task.  I've seen sucessful code in silver light/flex api, but can't seem to make it work in the javascript api.

I've tried a couple of different approaches by using the dojo.deferred object returned by the identify task or breaking out the callbacks into a separate functions, but I'm apparently dense on how to control the flow of execution using dojo.  Any suggestions are appreciated. 

My test at the end of the forEach service loop, tab container hasChildren(), is ALWAYS false, even though I can see results in the content pane object and the tab container object are correct.  I understand why it is happening, but not versed enough in javascript in general to know how to fix the issue and process synchronously.



>>execution order

console.log("execution order");

dojo.forEach(loadedServices, function(service) {
    mapService = map.getLayer(service);
    layerids.length = 0;                                                                    //reset layerids array
    if (mapService.visibleLayers.length > 0) {                                      //does service have visible layers?
        url = mapService.url;                                                             //get url of service
        identifyTask = new esri.tasks.IdentifyTask(url);                          //ceate new identify task 
        identifyParams.layerIds = mapService.visibleLayers;                    //other params set in init function
        //var idDeferred = identifyTask.execute(identifyParams);              //tried using return deferred object
        //idDeferred.then(function(idResults) {                                     //results the same, not right approach

            function(idResults) {
                //populate object for service and visible layers
                if (idResults.length>0) {
                    console.log("2");                                                      //execution order, understand it is async
                    cp = new dijit.layout.ContentPane ({                          //create a new tab for each service results
                    title: service,
                    style: "height:100px; width: 300px; color:#000000 background-image:none;background-color:transparent;"
                    addToCP(idResults);                                               //process results into contentPane
                    tc.addChild(cp);                                                    //add contentPane to tab container 
                    console.log(tc.hasChildren());                                  //is true....
            function(err) {
                console.log("doIdentify: (" + + ") " + err.message);



if (tc.hasChildren()) {                                                    //is always false due to 
    map.infoWindow.setContent(tc.domNode);, map.getInfoWindowAnchor(evt.screenPoint));
} else {
    alert('No results found, please check layer visibility and try again.');