Implementing ActionScript 3 AsyncToken

Look for links to examples or documentation on how to implement a method that returns an AsyncToken.

Please note that this is not about using / consuming a method that returns an AsyncToken! I want to write such methods myself.

+4
source share
2 answers

The implementation of the method that AsyncToken returns is simple:

function doStuffLater():AsyncToken { var token:AsyncToken = new AsyncToken(null); // This method will randomly call the responder 'result' or 'fault' // handler. function doStuff() { var response:String = (Math.random() > 0.5)? "result" : "fault"; for each (responder:IResponder in (token.responders || [])) { // rememeber: this is equivilent to // responder.result(...) or responder.fault(...) responder[response]("Got a result!"); } } setTimeout(doStuff, 1000); return token; } 

Note that you cannot use the applyResult and applyFault , because they pass a Event to the responders when the responder, except for the result or error object.

+2
source

Swiz Environment There are some really cool ways to mock AsyncToken in the TestUtil class:

http://code.google.com/p/swizframework/source/browse/trunk/src/main/flex/org/swizframework/util/TestUtil.as

Brian Kotek has a very informative blog post on how to use it to simulate server calls using delegate layouts:

http://www.briankotek.com/blog/index.cfm/2009/3/16/Swiz-Part-5-Simulating-Server-Calls-with-a-mock-AsyncToken

0
source

All Articles