From 643f41af5ff64405f0860cb4c86aaf733aa28cab Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Fri, 6 Aug 2021 21:13:58 +0200 Subject: [PATCH] More efficient sleep() implementation --- util.lua | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/util.lua b/util.lua index 852b933..b3fc4f7 100644 --- a/util.lua +++ b/util.lua @@ -1,9 +1,5 @@ function lua_async.yield() - local co = assert(coroutine.running(), "yield called outside of an async function") - - setTimeout(lua_async.resume, 0, co) - - coroutine.yield() + lua_async.sleep(0) end function lua_async.kill_thread() @@ -11,9 +7,10 @@ function lua_async.kill_thread() end function lua_async.sleep(ms) - await(Promise(function(resolve) - setTimeout(resolve, ms) - end)) + local co = assert(coroutine.running(), "sleep called outside of an async function") + setTimeout(lua_async.resume, ms, co) + + coroutine.yield() end function lua_async.run() -- 2.44.0