AnsweredAssumed Answered

How to nest dojo async calls and wait until all are complete

Question asked by sholyoak on May 19, 2015
Latest reply on May 20, 2015 by sholyoak

I'm trying to query address points from two FeatureLayer objects for one or more parcel polygons. The user will select the parcel polygons on the map, and behind the scenes, the address points will be selected. Once all the address points are selected, then I need to make an API call. I've written some code, shown below, and I'm close, but my outer "all" promise is not calling the API as expected. How do I do nested async calls like this, and have all complete before calling my API? Thanks!

 

define(["dojo/_base/declare", "dojo/promise/all", "dojo/_base/lang", "dojo/_base/array", "esri/tasks/query"],
function (declare, all, lang, array, Query) {
    var sdcAddressSelector = declare(null, {
        constructor: function () {
            this.addresses = [];
        },
        addAddressesToList: function(response) {
            response = array.filter(response, function (r) {//filter out any failed tasks
                return r.length > 0;
            });

            array.forEach(response, function (r) {
                this.addresses.push(r);
            });
        },
        callAPI: function () {
            //api calls here
        },
        executeSelect: function (selectedParcels) {
            var _this = this;
            var selectList = array.map(selectedParcels, function (parcel, index) {
                qry = new Query();
                qry.geometry = parcel.feature.geometry;


                //async loop over the address layers to get the  addresses that intersect the current parcel
                var addressList = array.map(esriMap.addressServices, function (layer, index) {
                    layer.queryFeatures(qry, lang.hitch(_this, _this.addAddressesToList));
                });
                return all(addressList);
            });


            return all(selectList, lang.hitch(this, this.callAPI));
        },
    });
    return sdcAddressSelector;
});

Outcomes