1 Reply Latest reply on Feb 8, 2010 1:41 PM by msakrejda

    Asynchrounous lookup , design pattern

    UbuntuPenguin Level 4

      TL;DR How do you handle a system that can be asynchronous and synchronous depending on its state.

      Hello Everyone ,

        I have a small problem and was wondering if anyone could give me some guidance how this is solved at various places.  Here goes .

      I have some objects whose values are set by using a dictionary like object.  The dictionary works as follows.  When given the key value from the value object , it will check to see if the value has already been loaded.  If so , it will return the matching value , if not , it will query a service for the the correct value for the key.

        My question is how would you solve this , or is there already a pattern out there dealing with this in the Flex/AS3 world.


      Sincerely ,


        • 1. Re: Asynchrounous lookup , design pattern
          msakrejda Level 4

          You can always design an asynchronous API, but execute it synchronously if the data is already available. E.g.,


          [Event(name='defined', type='com.example.DefinitionEvent')]

          public class RemoteDictionary {

              private var _cached:Object;

              private var _remoteSvc:RemoteObject;

              public function RemoteDictionary() {

                  _cached = {};

                  _removeSvc = new RemoteObject('dictionaryDestination');

                  _remoteSvc.addEventListener(ResultEvent.RESULT, handleResult);



               * Define given term. When the definition is available, the 'defined' event is dispatched


              public function define(term:String):void {

                  if (term in _cached) {

                      dispatchEvent(new DefinitionEvent('defined', term, _cached[term]));

                  } else {

                      var token:AsyncToken = _remoteSvc.define(term);

                      token.term = term;



              private function handleResult(e:ResultEvent) {

                  var term:String = e.token.term;

                  _cached[term] = e.result;

                  dispatchEvent(new DefintionEvent('defined', term, e.result));




          Error handling, etc., left as an exercise for the reader.