[AS3] Nested function and asynchronous model

In early days I’m used to synchronous programming model, everything can be called sequentially so I don’t have to deal with callback, I can write the code very neatly. When I meet AS3, I learn much about asynchronous functions, callbacks and event handlers.  AS3 uses asynchronous model so extensively, almost everything that needs “loading time” is asynchronous, like URLLoader, Loader, SQLStatement, and more. On one side it makes my job easier because I don’t need to create a background process (as in Java / C#) to do the loading in background and prevent the application from hanging. On the other side it makes the code I write pretty messy.

Imagine a case where I need to do multiple INSERT statement to an SQLite database and do something when all insertions finish. I can either 1) do all insertions sequentially and handle all success callbacks. Then when the number of callbacks equals the number of insertions, I continue. Or 2) do first insertion, wait callback, insert the next, wait callback, and so on until the last callback. In either case, I need to add a new object variable to count the number of callbacks. That makes the code more unclean because the variable should only be used temporarily.

This is where I find nested function to be really useful. I have known that AS3 has nested function since way back then, but I prefer not to use it in my code for the sake of clean and more readable code. However in this case of multiple asynchronous functions, I find no better way to do it than nested function. This is due to one magical property of nested function: it preserves the local variable of its parent function, even across asynchronous calls.

So for example, if I want to do multiple insert to SQLite using method 2) above, the code will look something like this.

function insertAll(){
  var i:int = 0, max:int = 10;
  var statement:SQLStatement = new SQLStatement();
  statement.sqlConnection = this.connection;
  function insert(){
    if (i < max){
      statement.text = "INSERT INTO table VALUES ("+i+")";
      statement.execute(-1, new Responder(insert));
      i++;
    } else {
      //do something after
    }
  }
  insert();
}

In function insertAll(), the value of local variable “i” will be preserved across calls to nested function insert(). So I don’t need to add any new object variable. In this case I find asynchronous model and nested function are meant to be used together, maybe that’s actually the idea behind them anyway.

Advertisements
This entry was posted in Programming and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s