AS3 - 如何进行异步调用的同步加载? [英] AS3 - How to do a synchronous load of an asynchronous call?

查看:35
本文介绍了AS3 - 如何进行异步调用的同步加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 Web 服务异步加载用户对象的函数.

I have a function that loads a user object from a web service asynchronously.

我将此函数调用包装在另一个函数中并使其同步.

I wrap this function call in another function and make it synchronous.

例如:

    private function getUser():User{
            var newUser:User;
            var f:UserFactory = new UserFactory();

            f.GetCurrent(function(u:User):void{
                newUser = u;
            });

            return newUser;
        }

UserFactory.GetCurrent 看起来像这样:

UserFactory.GetCurrent looks like this:

public function GetCurrent(callback:Function):void{ }

但我的理解是不能保证当这个函数被调用时,newUser 真的会成为新用户??

But my understanding is there is no guarantee that when this function gets called, newUser will actually be the new user??

你如何在 Flex 中实现这种类型的返回功能?

How do you accomplish this type of return function in Flex?

推荐答案

这种方式很疯狂.

说真的,你最好不要试图将异步调用强制到某种同步架构中.了解事件处理系统如何对您有利,并为结果事件添加处理程序.事实上,这是直接来自 flexcoders 常见问题解答的建议:

Seriously, you're better off not trying to force an asynchronous call into some kind of synchronous architecture. Learn how the event handling system works in your favour and add a handler for the result event. In fact, here's the advice straight from the flexcoders FAQ :

Q: How do I make synchronous data calls?

A: You CANNOT do synchronous calls. You MUST use the result event. No,
you can't use a loop, or setInterval or even callLater.  This paradigm is
quite aggravating at first. Take a deep breath, surrender to the
inevitable, resistance is futile.

There is a generic way to handle the asynchronous nature of data service
calls called ACT (Asynchronous Call Token). Search for this in
Developing Flex Apps doc for a full description.

这篇关于AS3 - 如何进行异步调用的同步加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆