# Asynchronous code?

**URL:** <https://forum.kirupa.com/t/asynchronous-code/331450>\
**Category:** flash\
**Created:** [August 3, 2013, 5:50pm UTC](https://forum.kirupa.com/t/asynchronous-code/331450 "2013-08-03T17:50:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pip9ball](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@pip9ball](https://forum.kirupa.com/u/pip9ball)\
**Post date:** [August 3, 2013, 5:50pm UTC](https://forum.kirupa.com/t/asynchronous-code/331450/1 "2013-08-03T17:50:40Z")

</div>

Hello All,

I’m working on an application which loads/saves data from a mySQL database. I’m storing and fetching data via PHP and XML data sets. I’m running into some problems with code from the main stage running synchronously when in fact I need data from the first PHP call to be available before continuing. Please take a look at the following code sample and let me know what I’m doing wrong.

Thanks,

Pip

```auto
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;

var xml:XML;
order_lbl.text = "foo";
var loadedPriceGroup:int;
var loadedBoatId:int;

## Main - Fetch the order id
var urlRequest:URLRequest = new URLRequest("http://localhost/~pip9ball/test/php/fetch_order_id.php");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);
trace("myPriceGroup: " + loadedPriceGroup);

// Fetch GEL XML, based off of order_id from previous call
var phpGelVars:URLVariables = new URLVariables();
var gelurlRequest:URLRequest = new URLRequest("http://localhost/~pip9ball/test/php/fetch_gels_by_id.php");
gelurlRequest.method = URLRequestMethod.POST;
/*
attach the php variables to tURLRequest
*/
            
gelurlRequest.data = phpGelVars;
            
var gelurlLoader:URLLoader = new URLLoader();
gelurlLoader.dataFormat = URLLoaderDataFormat.TEXT;
gelurlLoader.addEventListener(Event.COMPLETE, gelurlLoader_complete);
phpGelVars.boat_id = loadedBoatId;
trace("boatidMain: " + loadedBoatId);

gelurlLoader.load(gelurlRequest);

# Gets the gelcoat data
function gelurlLoader_complete(evt:Event):void {
    var xmlGel:XML = new XML(evt.target.data);
    trace(xmlGel);
}
## does something with the returned XML file
function urlLoader_complete(evt:Event):void {
    var xml:XML = new XML(evt.target.data);
    trace(xml);
    trace("Order ID: " + xml.row.od_id);
    order_lbl.text = xml.row.od_id;
    trace("Price Group: " + xml.row.price_group);
    loadedPriceGroup = xml.row.price_group;
    loadedBoatId = xml.row.boat_id;
    trace("boatid: " + loadedBoatId);
}

```

My call to the

```auto
gelurlLoader.load(gelurlRequest);

```

is getting called before

```auto
loadedBoatId = xml.row.boat_id;

```

is being executed.

I have an event listener on the completed event from the php call, however the urlLoader\_complete function seems to need an completed event as well, but I’m unsure on how to do this.

Any advise is greatly appreciated.
