views:

403

answers:

6

How can I listen for function completion?

let's say I have this processFile function, which is triggered in Class Constructor with another event listener.

private function processFile(e:event):void
{
//do whatever is this function suppose to do
}

now I want to listnen for it's completion

When I use

processFile.addEventListener(Event.Complete, anotherCoolFunction);

I get an Errorr

Error: 1061 posssibly undefined method AddEventListener through a stati type function

+3  A: 

Ya... no :)

You can only listen for events on objects who implements the IEventDispatcher interface, things like Sprite or MovieClip, or EventDispatcher.

There are a couple of solution available to you though, you could simply call the next function after you call processFile. When you call a function the next line wont execute until it is done. so in the following scenario:

processFile();
anotherCoolFunction();

You can be sure that anotherCoolFunction will never execute until processFile is completed.

Hope that helps.

Tyler.

Tyler Egeto
I hoped for more sophisticated way of handling this. So last line it is.
dd
The "sophistication" is in the simplicity.
Tyler Egeto
+1  A: 

What you're trying to do is not possible using addEventListener. Functions are not subclasses of the EventDispatcher class (the base class that provides the addEventListener method).

I'm failing to see why you would want to do something like this tho. If you have a function and you want to know when it's done executing, simply place anotherCoolFunction() directly after the call to processFile().

If processFile() is called as a callback to an event listener, simply place the call to anotherCoolFunction() in the last line of processFile().

Lior Cohen
+1  A: 

A function process is syncronous, so the last line of your function would indicate that it has finished. You could directly call another function, or do a dispatchEvent() (you need to extend EventDispatcher though, that's why you get an error ;)).

Cay
The problem, though, is that if you're in the last line of a function, you're still executing that function. So, the function has not really completed yet, which is what the OP asked...
Juan Pablo Califano
A: 

I would listen for a dispatchEvent() that is on the last line in the function so you will need to make the class it is in extend Sprite or MovieClip. By the time the event is handled the function would have completed. If you call another function from the processFile function then it will not be finished since it's still on the call stack.

Allan
+2  A: 
class SomeClass
{
    public function SomeClass()
    {
        this.addEventListener("fileProcessComplete", anotherCoolFunction);

        processFile();
    }

    private function processFile():void
    {
        //do whatever is this function suppose to do
        dispatchEvent(new Event("fileProcessComplete"));
    }

    private function anotherCoolFunction(evt:Event):void
    {
        //do whatever is this function suppose to do
    }
}
Jeremy White
+1  A: 

The Jeremy approach is the right one.. You also could do something like this if you don't want to use 'listeners'

class SomeClass
{
    public function SomeClass() {
     var obj = this;
        this.processFile(function(){
      obj.anotherCoolFunction(this);
     });
    }
    private function processFile(method:Function):void
    {
        //do whatever is this function suppose to do

     method.apply({some_var_name: 'hello'});
    }

    private function anotherCoolFunction(data):void
    {
        //do whatever is this function suppose to do

     trace(data.some_var_name) // => 'hello'
    }
}
Alvaro Talavera
I've never seen the use of a function in that manner. I'll have to remember that in the future -- it will probably end up being useful.
Jeremy White