Home > other >  get var from inside a function to return it AS3
get var from inside a function to return it AS3

Time:06-07

I have this static public function berechne() and I want this function to return data. But data is inside another function, so I can't access it outside the function to return it. I also can't let the second function away, because it is necessary to receive the fetched data after Event.COMPLETE.

How can I return data?

berechne():

static public function berechne(value1: String): Object {
    var request: URLRequest = new URLRequest('http://localhost/whatever.json');
    var loader: URLLoader = new URLLoader();

    loader.addEventListener(Event.COMPLETE, responseReceived);
    loader.load(request);

    function responseReceived(event: Event): void {
        var data:Object = JSON.parse(loader.data); //I would like to return this ...
    }
    return data; //... here
}

Thank you!

CodePudding user response:

You can try this setup:

static public function berechne(value1: String): Object 
{
    var request: URLRequest = new URLRequest('http://localhost/whatever.json');
    var loader: URLLoader = new URLLoader();

    var data:Object; //# define outside of function
     
    loader.addEventListener(Event.COMPLETE, responseReceived);
    loader.load(request);

    function responseReceived(event: Event): Object //# set Object as return type
    {
        data = JSON.parse(loader.data); //I would like to return this ...
        return data; //... here
    }
   
}

Another way is to just avoid the Return part and instead use the function to update your Object directly (as a function parameter).

In this example,
Your Object that should be updated by a return is now passed in as a parameter value2:

static public function berechne(value1: String, value2: Object): void
{
    var request: URLRequest = new URLRequest('http://localhost/whatever.json');
    var loader: URLLoader = new URLLoader();

    loader.addEventListener(Event.COMPLETE, responseReceived);
    loader.load(request);

    function responseReceived(event: Event): void
    { value2 = JSON.parse(loader.data); }
   
}

CodePudding user response:

You have to somehow work around the asynchronous behavior involved in Loader.load(), otherwise you just cannot retrieve your "data" when your berechne() is called. The code that uses that data should either first check if the data was actually loaded, then if not, bail out somehow. I would do like this:

class Your {
  static private Object data=null;
  static private Boolean dataLoaded=false;
  static private String dataJson="";
  public static function getData():Object {
    if (!Your.dataLoaded) return null;
    return Your.data;
  }  
  public static function berechne():void // note it's void, you can't expect data to be returned right away
  {
    if (Your.dataJson!=""){ //perhaps you've already loaded the data?
      Your.data=JSON.parse(Your.dataJson); // reset with preloaded data
    } else {
      // else just load. Or ignore that "if" and plain load, perhaps you want new values in your program
      var request: URLRequest = new URLRequest('http://localhost/whatever.json');
      var loader: URLLoader = new URLLoader();

      loader.addEventListener(Event.COMPLETE, responseReceived);
      loader.load(request);

      function responseReceived(event: Event): void
      { 
        Your.dataJson= event.target.data; // you have no "loader" here!
        Your.data=JSON.parse(Your.dataJson);
        Your.dataLoaded=true; // set flag "data ready"
      }
    }
  } // end berechne
} // end class
  • Related