Waiting on request to finish

Okay, I know there’s a simple answer to this, and I’m simply forgetting something basic, but…

In my app, I’m making a series of calls to a web service. Like all HTTP requests, the calls are asynchronous. The trouble is, I need to hold up the program and wait on results.

Is there some nice way to tell when a thread is complete?

I’m doing
MakeARequest() which passes results to SuccessfulReturn()
NextFunction()

But NextFunction needs to check if it got data from SuccessfulReturn, and it charges ahead before the request completes.

Should be easy, but I’m in brain lock.

@Mark - use a variable, as in

state="downloading"
http.request( ...., SuccessfulReturn)

function SuccessfulReturn()
    state="done"
    etc
end

function draw()
    if state=="downloading" then 
        --put wait message on screen
    else
        --do something else
    end
    etc

You can also write a class that would take a list of requests and callback functions and then have the code fire off each request in turn so that each subsequent request is triggered when the previous one returns.

Alternatively you could fire them all off at the same time and set a counter equal to the number of requests made and use the same success callback for each one and then decrement the counter so that you know when the last one finishes.

Search youtube for Node.js tutorials as both of these concepts are discussed at length as this is often how Node.js servers are setup

@Ignatz Thought of that, but the code I want to execute needs to run once, and ideally needs to run as part of the setup(). I fire off a series of service calls, most of which request data from lookup tables. If they fail I fallback to some default values, but really, the app can’t move until I get some results, good or bad, and some of these results affect the appearance of the whole app. So it would be much neater to treat the calls as synchronous.

That said, I’m currently working it much the way you indicated. Actually, using a counter that tells me if I have all the results, and the app doesn’t move out of “start up screen” until the results are done.

It’s just… kind of messy.

@Mark - unfortunately, it’s the only way.

You are waiting on an asynchronous callback, and the only time Codea polls for that event is when draw runs. So you have to let draw keep running.

I don’t think of it as bad. Not if you think of draw as being an event manager, and not just a drawing function.

@Mark Here’s something that I use. I have it setup to get pictures since I don’t know what you’re requesting. I also include some requests that are errors and show the clear button image. I tried to do all of this in setup, but it doesn’t work because the loop in setup won’t allow any other functions to execute until setup is complete. So the loop needs to be in draw.
Also, I don’t know what you’re doing in draw so I just display the images.


function setup()
    reqTab={'http://twolivesleft.com/logo.png',
            'http://www.freeimageslive.com/galleries/objects/tickets/pics/sign01948.jpg',
            'http://twolivesleft.com/logo.png',
            'http://www.freeimageslive.com/galleries/objects/general/pics/orangecovers.jpg',
            'http://twolivesleft.com/logo.png',
            'http://www.freeimageslive.com/galleries/objects/general/pics/glasslamp4.jpg',
            'http://this causes an error',
            'http://twolivesleft.com/logo.png',
            'http://this causes an error',
            'http://www.freeimageslive.com/galleries/home/playroom/pics/objects00016g.jpg',
            'http://www.freeimageslive.com/galleries/objects/general/pics/objects00453.jpg',
            'http://twolivesleft.com/logo.png',
            'http://www.freeimageslive.com/galleries/objects/general/pics/star0826.jpg',
            'http://this causes an error' }
    imgTab={}
    done=false
    requestGet=true
end

function draw()
    background(40, 40, 50)
    if not done then
        getimages()
    end
    for a,b in pairs(imgTab) do
        sprite(b,WIDTH/2,HEIGHT-50*a,50)
    end
end

function getimages()
    if requestGet then
        requestGet=false
        getImg()
    end
end

function getImg()
    print("getting image ",#imgTab+1)
    http.request(reqTab[#imgTab+1],gotImg,getFailed) 
end

function gotImg(img,status,headers)
    table.insert(imgTab,img)
    getNext()
end

function getFailed()
    table.insert(imgTab,readImage("Cargo Bot:Clear Button"))
    getNext()
end

function getNext()    
    if #imgTab>=#reqTab then
        done=true
        print("done")
    else
        requestGet=true
    end    
end