I program on AS3.

public static function isUserExist (): Boolean { var request:URLRequest = new URLRequest("https://ΠΊΠ°ΠΊΠΈΠ΅-Ρ‚ΠΎ_Π±ΡƒΠΊΠΎΠ²ΠΊΠΈ); request.method = URLRequestMethod.GET; var loader:URLLoader = new URLLoader(); loader.addEventListener(Event.COMPLETE, completeLoad); loader.load(request); function completeLoad (): Boolean { if (loader.data != "null") return true; else if (loader.data == "null") return false; loader.removeEventListener(Event.COMPLETE, completeLoad); } } 

I load the data from the server and, depending on it, should return a boolean value to the function that called the isUserExist () method. Can this be done somehow?

    2 answers 2

    ActionScript 3 does not support synchronous code stopping. In completeLoad (), set some Boolean flag and read it.

      In this case, you should approach the issue using your events:

       public function isUserExist():void { //Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° слСдуСт ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ var request:URLRequest = new URLRequest("https://ΠΊΠ°ΠΊΠΈΠ΅-Ρ‚ΠΎ_Π±ΡƒΠΊΠΎΠ²ΠΊΠΈ"); request.method = URLRequestMethod.GET; var loader:URLLoader = new URLLoader(); loader.addEventListener(Event.COMPLETE, completeLoad); loader.load(request); } private function completeLoad(e:Event):void { if (loader.data) { //Π― Π½Π΅ ΡƒΠ²Π΅Ρ€Π΅Π½, ΠΌΠΎΠΆΠ΅Ρ‚ Π»ΠΈ свойство URLLoader#data Π±Ρ‹Ρ‚ΡŒ null ΡƒΠΆΠ΅ послС Event#COMPLETE dispatchEvent(new MyEvent(MyEvent.SUCCESSFULL)); } else { dispatchEvent(new MyEvent(MyEvent.FAILED)); } } 

      And "outside" already handle events.

       var myClass:MyClass = new MyClass(); myClass.addEventListener(MyEvent.SUCCESSFULL, successfullHandler); myClass.addEventListener(MyEvent.FAIL, failHandler); myClass.isUserExist(); *** private function successfullHandler(e:MyEvent):void { } private function failHandler(e:MyEvent):void { } 

      You must first create a new class MyEvent, inherited from the Event. Carefully consider the values ​​of the constants, since They can "cross" with existing events.

       public class MyEvent extends Event { public static const SUCCESSFULL:String = "mySuccessfull"; public static const FAIL:String = "myFail"; public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); } } 
      • Thanks for the answer, in the end I came to this. - MrPitty