Skip to content

Instantly share code, notes, and snippets.

@creationix creationix/error-handling.lua Secret
Last active Feb 17, 2016

Embed
What would you like to do?
coroutine.wrap(function ()
local success, err = xpcall(function ()
-- Do all your logic here that may throw an error
end, debug.traceback)
if not success then
handleError(err)
end
end)()
local function async(fn, callback)
return coroutine.wrap(function ()
callback(xpcall(fn, debug.traceback))
end)()
end
-- Usage
async(function ()
-- Do coroutine blocking code here
end, function (success, result)
-- When it finishes, the result will be here.
end)
-- setTimeout(ms, callback)
function sleep(ms)
local thread = coroutine.running()
setTimeout(ms, function ()
coroutine.resume(thread);
end)
return coroutine.yield()
end
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.