views:

521

answers:

2

Looking for an example or documentation links as to how to implement a method returning AsyncToken.

Note this is not about using/consuming a method returning AsyncToken! I wish to write such methods myself.

+1  A: 

Implementing a method that returns an AsyncToken is simple:

function doStuffLater():AsyncToken {
    var token:AsyncToken = new AsyncToken(null);

    // This method will randomly call the responder's '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 can't actually use the applyResult and applyFault methods, because they pass the responders an Event, when the responder except the result or fault object.

David Wolever
A: 

Swiz framework's TestUtil class has some really cool ways for mocking AsyncToken behavior:

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 mock delegates:

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

Karthik