I haven't been following this thread, but the sample code below can actually be 
shortened to something like this:

    save(xml).addResponder(new AsyncResponder(handleResult, handleFault));

No AsyncToken in the code and rather than new'ing the responder, if you always 
want to direct results/faults to a consistent pair of handler functions you 
could set up the AsyncResponder earlier and just pass in a ref.

   save(xml).addResponder(saveResponder);

The only reason to actually get a ref to the returned AsyncToken is if you want 
to add some dynamic properties to it that will help drive your handling of the 
eventual async result/fault (the token - and any custom state you've tagged it 
with - is accessible within your result/fault callbacks when they execute).

But for scenarios like that where I want to hang onto some data from the call 
site, I often find it simpler to take advantage of the variable/scope capture 
that a closure provides by defining my result/fault callbacks as inline lambda 
functions rather than manually "capturing" state by copying a portion of it 
into properties of the AsyncToken. So something like:

    save(xml).addResponder(new AsyncResponder(
                                              function(...   , // inline result 
handler function
                                              function(...     // inline fault 
handler function
                                             ));

Rather than new'ing an HTTPService for every call you make and trying to manage 
adding/removing event listeners (which will prevent instances from being GC'ed) 
in your wrapper I'd recommend just following the first convention I list above 
for each call: someMethod(args).addResponder(new AsyncResponder(handleResult, 
handleFault));

Best,
Seth

From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of Mark 
Carter
Sent: Wednesday, December 10, 2008 7:23 PM
To: [email protected]
Subject: Re: [flexcoders] Best practice for calling asynchronous functions?


Thanks for all the responses.

I hadn't really looked into the ASyncToken until now. However, for me it
seems that using the ASyncToken would be limited to the implementation of
the, for example, save(XML, Function, Function) method.

The calling code doesn't need to know about it. In my opinion this is neater
than something like:

var asyncToken:ASyncToken = save(xml);
asyncToken.addResponder(...

Also, I don't like adding responders after the call has been made. I know it
works, but still...

Maybe I should start a new topic for this next question, but...

...in my implementation, I create a new HTTPService for each call. Any ideas
how (in)efficient this is? As you can imagine, it keeps the implementation
much simpler. No need for the ASyncToken. Just add new listeners each time a
call is made. Everything is garbage collected..... Oh, hang on, what keeps a
reference to the HTTPService?????
--
View this message in context: 
http://www.nabble.com/Best-practice-for-calling-asynchronous-functions--tp20930596p20948799.html
Sent from the FlexCoders mailing list archive at Nabble.com.


Reply via email to