AS3 - 如何同步加载异步调用?

1 apache-flex actionscript-3

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

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

例如:

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

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

            return newUser;
        }
Run Code Online (Sandbox Code Playgroud)

UserFactory.GetCurrent看起来像这样:

public function GetCurrent(callback:Function):void{ }
Run Code Online (Sandbox Code Playgroud)

但我的理解是,无法保证当调用此函数时,newUser实际上将成为新用户?

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

inf*_*ris 8

这种疯狂的方式.

说真的,你最好不要强制异步调用某种同步架构.了解事件处理系统如何对您有利,并为结果事件添加处理程序.事实上,这是直接来自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.