Is it possible to use http requests in a way that is blocking, so you don’t have to use callbacks and can use something like local response = http.request()instead?
I’m writing an abstraction for bundled files, which I want to work for html along with the other usual platforms. When you need to use callbacks for the http request that will force the users of the abstraction to use callbacks as well, since I can’t (as far as I know) return them the data in a simple local data = bundle_abstraction.read("file.file").
It is possible to do if you are running the code within a coroutine:
local function http_sync(url, method, headers, post_data, options)
local co = coroutine.running()
assert(co, "You must run this withing a coroutine")
http.request(url, method, function(self, id, response)
coroutine.resume(co, response)
end, headers, post_data, options)
return coroutine.yield()
end
-- example
function init(self)
-- wrap a function in a coroutine and run it
coroutine.wrap(function()
local response1 = http_sync(url1, "GET")
local response2 = http_sync(url2, "GET")
end)()
end