Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active February 17, 2016 02:31
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 creationix/19b9d8cefe55d946807c to your computer and use it in GitHub Desktop.
Save creationix/19b9d8cefe55d946807c to your computer and use it in GitHub Desktop.
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