如何立即获取RemoteObject的结果?

发布于 2024-11-04 19:33:55 字数 602 浏览 3 评论 0原文

我需要创建一个执行java方法并返回结果的函数。它是静态的,因为很多其他函数都会调用这个函数。所以我这样做了:

        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;
    }

但是函数返回 null,因为 valresult() 没有在 main 函数末尾被调用。我应该怎么做才能使 FKDescription() 返回来自远程对象的字符串?

坦克。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

固执像三岁 2024-11-11 19:33:55

这是因为 HTTP 调用是异步的,所以你必须想办法获取结果。您想要做的是将结果处理程序删除到它自己的函数中,以便它等待结果,然后对其执行某些操作。 不可能做您现在想要完成的事情,即立即返回值。

请查看此处了解如何进行异步调用

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.

爱本泡沫多脆弱 2024-11-11 19:33:55

正如 J_A_X 所说,所有 http 请求都是异步的,我建议以这种方式重构您的代码:

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);
    }
}

并且在调用者中,而不是:

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

您可以这样做:

FKDescription("something", otherFunction);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文