Skip to content

Instantly share code, notes, and snippets.

@daurnimator daurnimator/shittyscheduler.lua Secret
Last active Feb 17, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.