Apache – Flex RemoteObject Synchronous call

apache-flex

In the code below when I call chkAuthentication function from another function
the remoteObj.login (login function in my service file (.php)) is called after the remaining code in that function.
i.e., the loginStatus is returned from the function before the result-handler function loginResult is called. but my loginStatus is supposed to be set in loginResult function. It seems that the asynchronous behaviour is the culprit.
what should I do in order to get the loginResult function to complete first?
Please help me out. Thank you.

    private var loginStatus:Boolean;

     public function chkAuthentication(loginVOObj:LoginVO):String{
                remoteObj.login.addEventListener(ResultEvent.RESULT,loginResult);  
        remoteObj.login(loginVOObj);
        if(loginStatus == true){
            return displayName;
        }
        else{
            return 'fail';
        }
     }

     private function loginResult(result:ResultEvent):void
             {
        if(result.result == null){
            loginStatus=false;
        }else{
            loginStatus=true;

        }

     }

Best Answer

The chkAuthentication method should not return a String since it is asynchronous. Instead, just create an instance variable and set its String value in the loginResult method. You can then use a binding or dispatch an event to update the UI.