-
-
Save creationix/19b9d8cefe55d946807c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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