AS3 - How to do a synchronous load of an asynchronous call?

10,009

Solution 1

This way madness lies.

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.

Solution 2

See my answer here:

DDD and Asynchronous Repositories

Flex and Flash Remoting is inherently asynchronous so fighting against that paradigm is going to give you a ton of trouble. Our service delegates return AsyncToken from every method and we've never had a problem with it.

If you want to ensure that the application doesn't render a new view or perform some other logic until the result/fault comes back, you could do the following:

  1. Attach an event listener for a custom event that will invoke your "post result/fault code"
  2. Make the async call
  3. Handle the result/fault
  4. Dispatch the custom event to trigger your listener from #1

Bear in mind this going to lead to a lot of annoying boilterplate code every time you make an async call. I would consider very carefully whether you really need a synchronous execution path.

Share:
10,009
yantrab
Author by

yantrab

I'm a software developer with a long career. I have a good-sized family: one wife and three sons, one autistic. We live just outside of Boston. My interests, generally reflected in my blog, include: software engineering, parenting, math (especially visual aspects, such as geometry), politics, geeky recreations, language, and so on. If you like what I do, you can sponsor me on GitHub.

Updated on June 04, 2022

Comments

  • yantrab
    yantrab about 2 years

    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.

    For example:

        private function getUser():User{
                var newUser:User;
                var f:UserFactory = new UserFactory();
    
                f.GetCurrent(function(u:User):void{
                    newUser = u;
                });
    
                return newUser;
            }
    

    UserFactory.GetCurrent looks like this:

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

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

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

  • Zakus
    Zakus about 11 years
    Madness really? Is people who wrote this papers - cs.wustl.edu/~schmidt/patterns-ace.html, mad? There is nothing mad, to do something synchronously if there is no point to do it asynchronously, example when loading resources or initializing something, and where is no point to go to the next step if previous step is not finished yet.