1 Reply Latest reply on Dec 18, 2009 6:07 AM by Bruce.Lane06

    Asynchronous service calls and CallResponders

    tlaneadobe

      (This is a repost of a reply to another Relating 2 tables with one List Control.)

       

      Let me take a shot at explaining asynchronous calls. Most PHP/CF/web developers are used to calling functions in one way only: synchronously. In ActionScript a sychronous function call looks like this:

       

      var stuff = getStuff();

       

      Pretty straightforward. It's how you expect functions to work: you call them, and then immediately on line #2 of this example you have a value in the stuff variable to work with.

       

      But when it comes to calling remote functions on services sitting on a server somewhere, Flex calls them asynchronously. This is so that the Flex client can continue working while the request is pending. An aync call looks more like this (at least, the first part):

       

      responder.token = getStuffFromService();

       

      What this means is, "stuff" does not come back right away as the result of the function. Instead, what it returns is a kind of ticket. Sort of like ordering some food from a lunch counter and getting a receipt with a number on it. You don't get your food right away. Instead, you go hang out or do some other stuff while your order is pending, and then your number is called when it's ready. And then you "respond" to your number.

       

      Well, that's what a CallResponder is. When you "place your order", you hook up a CallResponder to it. When your order is ready (data comes back from the service call), the responder kicks into action. It fires a result event (or a fault if something went wrong), and it updates its bindable lastResult property.

       

      So, if you want to work with data from an async call, you can't simply do it on line #2 of the above snippet. You have to wait until the CallResponder for that call gets its result. If you create a databinding to lastResult, that will automatically update when the result is ready. But since you want to run some script on the result, you have to trigger that script from the CallResponder's result event handler.

       

      Does that help? Async is definitely a new concept for many web developers to wrap their heads around. But it lets you build some interesting UIs once you get the hang of it.

       

      Best,

      Tom