Hi everyone. I think I am missing about understanding how coroutines work.
I have searched forum and found some examples, but still could not make code work synchronously.
local co = coroutine.create(function()
local co = coroutine.running()
print('request is executed')
http.request("http://d.defold.com/stable/info.json", "GET", function(self, id, response)
print('result is fetched')
coroutine.resume(co, response)
end)
return coroutine.yield()
end)
local ok, err = coroutine.resume(co)
print('should not be executed before fetched result')
From my understand last print(‘should not be executed…’) should printed last, since we are pausing coroutine until we receive response from the http request. But it does not work like this.
The output, which I get is:
DEBUG:SCRIPT: request is executed
DEBUG:SCRIPT: should not be executed before fetched result
DEBUG:SCRIPT: result is fetched
Can somebody advice something about it?