AnsweredAssumed Answered

Problem with Deferred(s) in Loop

Question asked by JerryGarcia on May 27, 2015
Latest reply on May 28, 2015 by JerryGarcia

The following code will execute fine with only one deferred, but more than one, 'all.then()' never executes.

 

(Overall, I am trying to validate a set of geometries to ensure they are not too big before adding to the map)

 

Any help would be very much appreciated.  Thanks!

 

var defListArray = [];
array.forEach(layer.featureSet.features, function (feat) {
    var geom = feat.geometry;                    
    defListArray.push(that.validateFieldSize(geom));
});
                
//var defList = new DeferredList(defListArray); //Does not work with DeferredList as well; same behavior

all(defListArray).then(lang.hitch(this, function (results) {   
    //PROBLEM: This code only executes when there is only one Deferred in array
    // I want to know when
    console.log('deferred list', results);
}));

//...//

validateFieldSize: function(fieldGeom){
    var that = this;
    var deferred = new Deferred();

    this.geometryService = new GeometryService(this.geometryServiceURL);
    this.geometryService.on('areas-and-lengths-complete', lang.hitch(this, 'showVerifyiedFieldGeometryResults', deferred));

   //setup the parameters for the areas and lengths operation
    var areasAndLengthParams = new AreasAndLengthsParameters();
    areasAndLengthParams.lengthUnit = GeometryService.UNIT_FOOT;
    areasAndLengthParams.areaUnit = GeometryService.UNIT_ACRES;
    areasAndLengthParams.calculationType = 'geodesic';
    this.geometryService.simplify([fieldGeom], function (simplifiedGeometries) {
        areasAndLengthParams.polygons = simplifiedGeometries;
        that.geometryService.areasAndLengths(areasAndLengthParams);
    });

   //deferred.resolve('QUICK RESOLVE'); //'all().then()' executes if I comment this line out...
    return deferred;
},
showVerifyiedFieldGeometryResults: function(deferred, results){
    var acres = Math.floor(results.result.areas[0]);

    //NOTE: Check acres to ensure the feature is not too big... code omitted...

    console.log('geometry results'); //this line always executes so it appears something is wrong with the Deferreds...
    deferred.resolve('Verified Field Size: ' + acres);              
}

Outcomes