Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Last active February 17, 2016 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daurnimator/a0288074b747a4c26d16 to your computer and use it in GitHub Desktop.
Save daurnimator/a0288074b747a4c26d16 to your computer and use it in GitHub Desktop.
local coros = setmetatable({}, {__mode="kv"})
local function new_thread(func, onerror)
local co = coroutine.create(func)
local function resumer(...)
local ok, err = coroutine.resume(co, ...)
if not ok then
onerror(co, err)
end
end
coros[co] = resumer
return resumer
end
local function resumer()
local co = coroutine.running()
return coros[co]
end
function sleep(ms)
setTimeout(ms, resumer())
return coroutine.yield()
end
local t = new_thread(function(...)
print("A", ...)
sleep(1000)
print("B")
error("oops!")
end, function(co, err)
print("an error occured!", debug.traceback(co, err))
end)("myargs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment