]> git.lizzy.rs Git - lua_async.git/commitdiff
Move resume() to util.lua
authorElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 6 Aug 2021 20:01:53 +0000 (22:01 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 6 Aug 2021 20:01:53 +0000 (22:01 +0200)
async_await.lua
util.lua

index 97d4f7d4706696c477233d53d0581af85e9c17b3..6965688a81bc9847606fcef1cc236297dc3b227d 100644 (file)
@@ -1,14 +1,15 @@
-lua_async.async_await = {}
+function async(func)
+       return function(...)
+               local promise = Promise()
+               promise.__on_resolve = func
 
-function lua_async.resume(co)
-       local status, err = coroutine.resume(co)
+               local args = {...}
 
-       if coroutine.status(co) == "dead" or err then
-               lua_async.limiting.unset_limit(co)
-       end
+               lua_async.resume(coroutine.create(function()
+                       promise:resolve(unpack(args))
+               end))
 
-       if not status then
-               error("Error (in async function): " .. err)
+               return promise
        end
 end
 
@@ -25,19 +26,3 @@ function await(promise)
 
        return unpack(promise.values)
 end
-
-function async(func)
-       return function(...)
-               local promise = Promise()
-               promise.__on_resolve = func
-
-               local args = {...}
-
-               lua_async.resume(coroutine.create(function()
-                       promise:resolve(unpack(args))
-               end))
-
-               return promise
-       end
-end
-
index e7c6cbf2b216dfdb350258dddeb5cb3dab4b8152..7061c0619f379e189766f782ec57a1c9f63ab1eb 100644 (file)
--- a/util.lua
+++ b/util.lua
@@ -4,16 +4,28 @@ function lua_async.yield()
        end))
 end
 
-function lua_async.kill_thread()
-       coroutine.yield(true)
-end
-
 function lua_async.sleep(ms)
        await(Promise(function(resolve)
                setTimeout(resolve, ms)
        end))
 end
 
+function lua_async.kill_thread()
+       coroutine.yield(true)
+end
+
+function lua_async.resume(co)
+       local status, err = coroutine.resume(co)
+
+       if coroutine.status(co) == "dead" or err then
+               lua_async.limiting.unset_limit(co)
+       end
+
+       if not status then
+               error("Error (in async function): " .. err)
+       end
+end
+
 function lua_async.run()
        local last_time = os.clock()