http.Fetch problem

i am running this code but it doesn’t print something


local retrieved_code = ""

http.Fetch("https://www.google.com", 
    function(body, len, headers, code)
        retrieved_code = body
    end, 
    function(error)
        Msg("Could not connect to the server.")
    end
)

print(retrieved_code)

but it works like this;


local retrieved_code = ""

http.Fetch("https://www.google.com", 
    function(body, len, headers, code)
        retrieved_code = body
        print(retrieved_code)
    end, 
    function(error)
        Msg("Could not connect to the server.")
    end
)

somehow i need to get the retrieved_code variable out from this onSuccess function.

Received_code is set in an asynchronous callback. This means that you cannot guarantee that it will be set directly after passing the callback to a function. You should instead call print on received_code inside the callback where you are setting it

Edit: original post was changed while I was submitting my reply.

okay, thanks.

There’s a way to my synchronous? Yesterday I was trying. But did’nt found one.

No, the only way to use a variable that is set in an asynchronous callback is to use it in that callback or after.