Recipe 20.8 Distinguishing Among Results from Multiple Calls to a Single Service

20.8.1 Problem

You want to be able to distinguish among results from multiple calls to a single service function.

20.8.2 Solution

Use instances of a custom response class for each response object.

20.8.3 Discussion

When a result is returned from a Flash Remoting service function, Flash doesn't automatically tell you which service function invocation generated the result. Furthermore, due to the asynchronous nature of Flash Remoting, you cannot rely on results being returned to Flash in the same order in which the functions were invoked. However, you can distinguish between the results from calls to multiple service functions by using a separate instance of a custom class for each response object. Simply attach a custom property to each response object instance and check its value when the result from the service function is returned.

This solution uses a custom response class that takes an id parameter in the constructor. This allows you to create multiple instances of the class?one for each function invocation?and assign each one a unique id. Then, when the result comes back, you can distinguish between the results using the id property of the response object. For example:

// Define a constructor for a custom response object class that assigns an id 
// property to each instance.
function MyResponseObject(id) { = id;

// Define the onResult(  ) method for the response object class.
MyResponseObject.prototype.onResult = function (result) {
  // Process the result differently depending on the value of the id property.
  trace("The response came from the call with id " +;

// Call the same service function multiple times. In this example the service
// function, getUserInfo(  ), which is not shown, retrieves user information for
// different users based on a username parameter. In each case, use an instance of
// the MyResponseObject class as the response object. Assign each instance an id 
// corresponding to the username so that you can distinguish between them when the
// results are returned.
myService.getUserInfo(new MyResponseObject("Heather"), "Heather");
myService.getUserInfo(new MyResponseObject("Julie"), "Julie");
myService.getUserInfo(new MyResponseObject("Sarah"), "Sarah");

20.8.4 See Also

Recipe 20.7

    Part I: Local Recipes
    Part II: Remote Recipes