Last active
January 26, 2023 16:17
-
-
Save ChaunceyHoover/a75eca64141d98fbc897 to your computer and use it in GitHub Desktop.
Lua asynchronous and synchronous sleeping
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
function sleep(time, func, ...) | |
local now = os.time() | |
local thread = coroutine.create(func) | |
repeat until (os.time() - now > time) | |
coroutine.resume(thread, ...) | |
end | |
function asleep(time, func, ...) | |
coroutine.wrap(function() | |
local now = os.time() | |
local thread = coroutine.create(func) | |
repeat until (os.time() - now > time) | |
coroutine.resume(thread, ...) | |
end)() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I attempt a similar implementation, "repeat until" simply freezes the entire program, not only the coroutine.