I've been working with AS3 a lot over the last weeks and I've run into a situation that google hasn't been able to help with. The crux of the problem is that before I run a function I need to know that certain conditions have been met, for example loading my config.xml and putting it into a class variable. Where I run into trouble is that URLLoader is async so I can't put code after the load, the only thing I can do is put it in a lambda on the listener.
var conf:Object = new Object();
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, geoLoader, false, 0, true);
ldr.load(new URLRequest("http://ipinfodb.com/ip_query.php"));
function geoLoader (e:Event):void
{
var data = new XML(e.target.data);
conf.zip = data.ZipPostalCode.toString();
conf.city = data.City.toString();
conf.state = data.RegionName.toString();
}
Or slightly more concisely and I believe more understandable:
var conf:Object = new Object();
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, function (e:Event){
var data = new XML(e.target.data);
conf.zip = data.ZipPostalCode.toString();
conf.city = data.City.toString();
conf.state = data.RegionName.toString();
}, false, 0, true);
ldr.load(new URLRequest("http://ipinfodb.com/ip_query.php"));
This worked great for short bits of code, but I've got a bunch of functions that are relying on huge amounts of preconditions (this pollutes the namespace if I use the first method) or functions that rely on one bit of the stack (which means that I can't use the second method even though it's slightly more obvious to me).
So, my question is: How do flash professionals handle preconditions? I'm trying to find a solution that allows tons of preconditions as well as a solution that allows different orders of preconditions or just a few preconditions from a group.
Edit: I'm not sure I made it clear originally but the problem is that I'll have 5 preconditions which means that I end up with 5 of the above blocks. As you can imagine having 5 nested listeners makes for terrible spaghetti code. An example, one of my functions needs to know: when the config.xml is loaded, when the location is updated, when the current weather conditions have been loaded, and when the images for the current weather have been loaded. However there is another function that needs to know only when the config.xml is loaded and the location is updated. So it's a really complicated set but I'm sure that I'm not the only one out there that's had to deal with a complex set of events.
Also worth noting, I have looked at Senocular's Sequence.as but to the best of my understanding that's based on returns rather than events. But I could be wrong about this.
Thanks for any help you can offer!