Implement blocking http.request() ?

@iam3o5am good catch. so does it work now?

@iam3o5am It’s not that it’s frustrating, it’s just hard to give you an example when I don’t know everything you’re trying to do. Here’s another example of a program that does different things along with the http.request. Since I don’t know what all you want to do, I kind of just put in dummy routines.

function setup()
    func=menu
end

function draw()
    background(40, 242, 191, 255)
    fill(255,0,0)
    func()
end

function request()
    print("requesting data")
    http.request("http://google.com/home", success)
    func=wait
end

function success(data)
    print(data)
    print("response received")
    func=process
end

function menu()
    text("This is a menu screen",WIDTH/2,HEIGHT/2)
    text("Buttons could be here to do different things.",WIDTH/2,HEIGHT/2-50)
    text("Since I don't know what that is\
just tap the screen to start a download.",WIDTH/2,HEIGHT/2-100)
end

function process()
    text("Processing received data",WIDTH/2,HEIGHT/2)
    text("Since I dont know what you want to do\
for processing the data received",WIDTH/2,HEIGHT/2-50)
    text("just tap the screen to simulate end of processing to continue.",WIDTH/2,HEIGHT/2-100)
end

function continue()
    text("Here is a continue screen",WIDTH/2,HEIGHT/2)
    text("This is where whatever your program is going to do",WIDTH/2,HEIGHT/2-50)
    text("Since I dont know what that is\
just tap the screen for the menu again.",WIDTH/2,HEIGHT/2-100)
    output.clear()
end

function wait()
    text("Waiting for download",WIDTH/2,HEIGHT/2)
end

function touched(t)
    if t.state==BEGAN then
        if func==menu then
            func=request
        elseif func==process then
            func=continue
        elseif func==continue then
            func=menu
        end
    end
end

@iam3o5am ok, please try this one. The idea behind it is much simpler but the code has to be simplified yet. You create a variable for the response-data (and the response-status if you want to use it). Then start a blocking coroutine wait() which is blocking everything until the request variable got assigned, which again happens only after the request got a response from the server.

local http_status
local http_response
local thread_queue = {}

local function thread_update()
    if #thread_queue > 0 then
        if coroutine.status(thread_queue[1]) == "dead" then table.remove(thread_queue, 1)
        else coroutine.resume(thread_queue[1], thread_queue[1]) end
    end
end

local function exec(func, ...)
    local params = {...}
    local thread = function(self) func(self, unpack(params)) end
    table.insert(thread_queue, coroutine.create(thread))
end

local function listener(response, status)
    http_status = status
    http_response = response
end

local function process()
    print(http_response) -- do something with the http response data
    print("response received. done.")
    http_status = nil
    http_response = nil
end

local function wait()
    while not http_response do
        print("wait...")
        coroutine.yield()
    end
    process()
end

local function request()
    http.request("http://google.com/home", listener)
    print("request sent. wait...")
end

local function do_something_else()
    print("now continue doing other work...")
end

function setup()
    request()
    exec(wait)
    exec(do_something_else)
end


function draw()
    thread_update()
end