16
May

Flex Call Stack and callLater()

Ive used callLater() quite a few times before, but about a week ago a co-worker asked me if I knew exactly when callLater() was called. Well I think its 1 millisecond after the method executes, to which he re-asked if its after the method or after the block of code executes? Great question, and I honestly couldn’t with absolute certainty answer him so I went ahead and wrote this quick POC to prove the fact one way or another.

Basically what I’m doing here is adding an initialize handler and a complete handler which will fire in that order except the initialize handler also has a callLater() call in it so what I expect to see is that the initialize function will fire the callLater function will fire then finally the complete handler will fire.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	initialize="onInitialize()"
	creationComplete="onCreationComplete()">
 
	<mx:Script>
		<![CDATA[
 
			// What we expect here is a trace output of
			// Call 1 Call 3 Call 2, not Call 1 Call 2 Call 3
			// this proves callLater dispatches a call directly
			// 1 millisecond after the code black is done, that
			// contains the callLater,
			// not after all code blacks are done.
			private function onInitialize():void
			{
				trace("call 1");
				callLater(trace3);
			}
 
			private function onCreationComplete():void
			{
				trace("call 2");
			}
 
			private function trace3():void
			{
				trace("call 3");
			}
		]]>
	</mx:Script>
</mx:Application>

This proves to me with a concrete example that callLater gets fired 1 millisecond after the method its requested from fires.

There's 1 Comment So Far

Share your thoughts, leave a comment!