Getting a value from AsyncResponder

1795
2
04-13-2011 06:38 PM
ShannakaBeveridge
New Contributor II
Hi,

I'm having an issue with the code below. The issue is that "return name;" is running before "onResult", so it returns null even though the onResult function is getting the correct name. Is there anyway to get it to wait until onResult or onFault are complete?

private function getRel(geometry:Geometry,lyr:String):String
   {
    var name:String;
    
    var query:Query = new Query();
    query.geometry = geometry;

    var featureLayer:FeatureLayer = map.getLayer(lyr) as FeatureLayer;
    featureLayer.queryFeatures(query, new AsyncResponder(onResult, onFault));
    
    function onResult(featureSet:FeatureSet, token:Object = null):void
    {
     if (featureSet.features.length > 0)
     {
      for each (var myGraphic:Graphic in featureSet.features)
      {
       if (myGraphic.attributes["NAME"])
       {
        name = name + myGraphic.attributes["NAME"];
        Alert.show("name =" + name);        
       }
      }      
     }
     else
     {
      //Alert.show("No " + lyr + " found. Please enter " + lyr + " name.");
     }    
    }
    
    function onFault(info:Object, token:Object = null):void
    {
     //Alert.show(info.faultString + "\n\n" + info.faultDetail, "queryTask fault " + info.faultCode);
    }
    
    return name;
   }


Cheers,

Shannaka
Tags (2)
0 Kudos
2 Replies
ReneRubalcava
Frequent Contributor
In a situation like that, you'd probably want to wrap that whole function into it's own class that extends EventDispatcher, then have the function dispatch the result "name". You could make a custom event or use a DynamicEvent to save some time.
0 Kudos
eddiequinlan
Occasional Contributor
I'm sorry if my question gets posted twice, but I don't see my first post on the thread.

I'm having a very similar problem, but I don't understand how to implement the eventdispatcher method.  Could you post a sample of your code either extending the EventDispatcher or the other way of a custom dynamic event.  I'm confused on this.

Sincerely,
Eddie
0 Kudos