Skip to content

Instantly share code, notes, and snippets.

@ChaunceyHoover
Last active January 26, 2023 16:17
Show Gist options
  • Save ChaunceyHoover/a75eca64141d98fbc897 to your computer and use it in GitHub Desktop.
Save ChaunceyHoover/a75eca64141d98fbc897 to your computer and use it in GitHub Desktop.
Lua asynchronous and synchronous sleeping
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
@Rakaesa
Copy link

Rakaesa commented Jan 26, 2023

When I attempt a similar implementation, "repeat until" simply freezes the entire program, not only the coroutine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment