简体   繁体   中英

Waiting for dojo/request/xhr to finish before returning array from dojo/-base/array.map

I am modifying the code below which was structured on the example at Display IdentityTask results in Popup .

var deferred = identifyTask.execute(identifyParams).addCallback(function (response) {
  return arrayUtils.map(response, function (result) {
    var feature = result.feature;
    dojoXhr("/trails/trailPopupUses.action",{
      query:{
        parkName: feature.attributes.PARK_NAME,
        trailName: feature.attributes.TRAIL_ASSOC,
      },
      preventCache: true
    }).then(function(data) {
      useString = data;
      return feature;
    }, function(err) {
      featureResultsContent.innerHTML = "An unexpected error occurred: " + error;
    });
  });
});

app.map.infoWindow.setFeatures([deferred]);

My issue is I can't figure out how to insure the app.map.infoWindow.setFeatures([deferred]); doesn't execute until the identifyTask.execute has finished initializing the deferred variable. I have tried using .then() in a couple of places but couldn't get it to work. The arrayUtils.map function returns an array of features which is what the app.map.infoWindow.setFeatures([deferred]); expects.

I did not make it clear that the arrayUtils.map is the dojo/_base/array.map() method. The issue was each iteration of arrayUtil.map needed to wait until the data returned from the dojoXhr call (which is 'dojo/request/xhr' ) and the arrayUtils.map needed to finish before the app.map.infoWindow.setFeatures() was called. My solution was to remove the return and use .then on the deferred variable. I also added sync: true to the dojoXhr .

                var featureArray;
            var deferred = identifyTask.execute(identifyParams);
            deferred.then(function (response) {
                featureArray = arrayUtils.map(response, function (result) {
                    var feature = result.feature;
                    dojoXhr("/trails/trailPopupUses.action",{
                        query:{
                            parkName: feature.attributes.PARK_NAME,
                            trailName: feature.attributes.TRAIL_ASSOC,
                        },
                        preventCache: true,
                        sync: true
                    }).then(function(data){
                        useString = data;
                    });
                    feature.setInfoTemplate(testTemplate);
                    return feature;
                });
                return featureArray;
            }).then(function(featureArray){
                app.map.infoWindow.setFeatures(featureArray);
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM