Using Async calls in a Util class
I have a Utility class that I want to put code in that I'm reusing over and over again. This includes Async.handleEvent calls. If I call an instance of the Util class from a [Test(async)] method, can I use the Async call in that other event?
When I tried this in my code, it said that it "Cannot add asynchronous functionality to methods defined by Test,Before or After that are not marked async" but my [Test] is marked as async. And when I was in the same class, I was able to have non-Test methods make Async calls.
Thanks for the help!
Mocked up Example (please note these are two separate files):
// Test.as
package
{
import TimerUtil;
public class Test
{
[Test(async)]
public function waitForTimer():void
{
TimerUtil.newTimer();
}
}
}
// TimerUtil.as
package
{
import flash.events.TimerEvent;
import flash.utils.Timer;
import org.flexunit.async.Async;
public class TimerUtil
{
private static var instance:TimerUtil;
private var timer:Timer;
private static const TIME_OUT:int = 1000;
public static function newTimer() : void
{
if (!instance) instance = new TimerUtil();
instance.timer = new Timer(1000, 5);
instance.timer.start();
instance.waitForFifthTick();
}
private function waitForFifthTick() : void
{
Async.handleEvent(this, instance.timer, TimerEvent.TIMER, handleTimerTick, TIME_OUT);
}
private function handleTimerTick ( evt:TimerEvent, eventObject:Object) : void
{
if ( timer.currentCount == 5 )
{
// We are at the Fifth click, move on.
}
else
{
Async.handleEvent(this, instance.timer, TimerEvent.TIMER, handleTimerTick, TIME_OUT);
}
}
}
}
Message was edited by: zyellowman2 I missed a comma. And a few "instance"s
