# Iterating Geometry Server Distance calculations on a Result Set

Discussion created by jkapellas on Aug 2, 2011
Latest reply on Aug 4, 2011 by jkapellas
I've searched through the forums and a bunch of dojo sites on Google, but haven't been able to find an answer for a problem that has stumped me for the past couple of days. So, I thought I'd just go ahead and ask straight out.

I am trying to write a function that calculates the distances between a point and each feature that falls within a buffer around that point. After performing the spatial query, the result set from the query is passed to the function that then returns an array of objects containing selected attributes from the features and the distance calculation. Here's a code snippet.

```

distanceParams.geometry1 = myMap.geocodePoint; //the point being buffered

var myAttributeArray = dojo.map(bufferQueryResultSet.features, function(res){

var attr = {};
attr.ID = res.attributes.ID;
attr.NAME = res.attributes.NAME;
distanceParams.geometry2 = res.geometry;
myMap.geometryServer.distance(distanceParams, function(d){
attr.DISTANCE = d;
}).then(function(){return attr;});
});
```

The function as written above returns myAttributeArray as an array of undefined objects. If I rewrite the last few lines as:

``` myMap.geometryServer.distance(distanceParams, function(d){
attr.DISTANCE = d;
});
return attr;
```

...then myAttributeArray comprises objects with only ID and NAME defined.

I also tried:
``` attr.DISTANCE = myMap.geometryServer.distance(distanceParams, function(d){
return d;
});
return attr;
```

...and that gave me an array of objects with ID and NAME defined and DISTANCE defined as a dojo.Deferred object.

I know that there must be something with dojo.Deferred that I'm overlooking, but I'm not sure what. Any assistance would be greatly appreciated.

Thanks!

Jeff Kapellas
California State Water Resources Control Board