ASyncToken example

ASyncToken in Flex allows you to add additional data to your Remote Object calls. Consider the AS3 application below.

Action Script Code

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="htp://" 
    minWidth="955" minHeight="600"
    <s:VerticalLayout />
      import Responders.BravoResponder;
      import mx.controls.Alert;
      import mx.rpc.AsyncToken;
      private var letter : String;
      protected function init(event:FlexEvent):void
          letter = 'A';
      protected function button1_clickHandler(event:MouseEvent):void
          var token : AsyncToken = ro.token_call(letter);
          token.letter = letter;
          token.addResponder(new mx.rpc.Responder(onResult, onFault));
          if(letter == 'C')
            letter = 'A';
          else if(letter == 'B')
            letter = 'C'
            letter = 'B';
      protected function onResult(event : ResultEvent) : void
          var myData   : String = event.result as String;
          var myLetter : String = event.token.letter;
"Return: " + myData, "Token: " + myLetter);
      protected function onFault(event:FaultEvent):void
"Crap, something went wrong");
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <s:RemoteObject id="ro"
                    source="Bravo" />
  <s:Label text="{letter}" />
  <s:Button label="token call"
            click="button1_clickHandler(event)" />

The application displays a letter - initiated to 'A' and a button. When you press the button, the value of the letter is passed to a Remote Procedure call and the letter is incremented to the next letter. The letters run in sequence of ABC.

A difference we see in this code from my other RPC examples is here:

var token : AsyncToken = ro.token_call(letter);
token.letter = letter;
token.addResponder(new mx.rpc.Responder(onResult, onFault));

We are adding the result and fault events to the actual token (object returned from a RemoteObject call), rather than coding it directly in a method tag (a property of the RemoteObject declared in the declarations section). See my other RemoteObject examples where I have done this.

ASyncToken is a dynamic class. That means I can 'add' properties to it at run-time, and in this case I am adding a property called letter and assigning it the parameter I passed to the remote procedure call (ro.token_call). What this means is that when my result event handler is called, I will have access to this token and the additional properties I set for it. If you examine the Flex code you will see how I have used the token.

PHP Code
The PHP code returns a string based on the letter that was passed to it. There is also a one-to-one relationship with the input parameter and what has been returned which will be very useful for our testing.

If we press the button 3 times in rapid succession then we are invoking 3 separate remote procedure calls passing ABC. Examining the PHP code we can see that passing ABC in rapid succession (about a second apart from each button call), should respond back with BAC.

PHP Code

class Bravo
public function __construct()
public function token_call($p_letter)
$statement = 'default';
  case 'A' : 
    $statement = 'a bit slow is A';
  case 'B' : 
    $statement = 'a bit quicker is B';
  case 'C' :
    $statement = 'even slower is C';
return $statement;

If we get BAC back from the server then I should expect the first Alert box to display the message a bit quicker is B with a token.letter value of 'B'. In fact that is exactly what I get as shown in the screen shot on the left.

If we had not used an ASyncToken then our result handler would have run 3 times but we would not have known which of our Flex calls had triggered that particular result handler. Using the token we were able to add additional properties which helped us identify which call the handler was responding to. An example of where an ASyncToken would be very beneficial is data paging with lists and data-grids where you are making RPC calls based on the scroller positon


Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.