NeoRamza
NeoRamza

Reputation: 1

How to get the result of a RemoteObject immediately?

I need to make a function that execute a java method and return his result. It is static becouse a lot of other functions will call this function. So I did this:

        public static function FKDescription(dest:String):String{
        var jRemote:RemoteObject = new RemoteObject();
        var s:String;
        jRemote.destination = dest;
        jRemote.getValues.addEventListener(ResultEvent.RESULT,valresult);
        jRemote.getValues();

        function valresult(event:ResultEvent):void{
            s = event.result as String;
        }

        return s;
    }

But the function returns null, because the valresult() was not been called at the end of main function. What shoud I do to make FKDescription() return the string that came from remoteobject?

Tanks.

Upvotes: 0

Views: 1152

Answers (2)

ALoR
ALoR

Reputation: 4914

as J_A_X said, all the http requests are async, i suggest to refactor your code this way:

public static function FKDescription(dest:String, callback:Function):String{
    var jRemote:RemoteObject = new RemoteObject();
    var s:String;
    jRemote.destination = dest;
    jRemote.getValues.addEventListener(ResultEvent.RESULT,valresult);
    jRemote.getValues();

    function valresult(event:ResultEvent):void{
        callback(event.result as String);
    }
}

and in the caller, instead of:

 ret = FKDescription("something");
 otherFunction(ret);

you can do this:

FKDescription("something", otherFunction);

Upvotes: 1

J_A_X
J_A_X

Reputation: 12847

That's because HTTP calls are asynchronous, so you have to way for the result. What you want to do is remove the result handler to it's own function so that it waits for the result, then does something with it. It is NOT possible to do what you're trying to accomplish right now, which is returning the value right away.

Check here on how to do async calls.

Upvotes: 2

Related Questions