aboutsummaryrefslogtreecommitdiff
path: root/util.lua
blob: b5c70436806a2d1cccc2af6af0c2bc404acd7ef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function lua_async.yield()
	local co = assert(coroutine.running(), "yield called outside of an async function")

	setTimeout(lua_async.resume, 0, co)

	coroutine.yield()
end

function lua_async.sleep(ms)
	await(Promise(function(resolve)
		setTimeout(resolve, ms)
	end))
end

function lua_async.run()
	local last_time = os.clock()

	while true do
		local current_time = os.clock()
		local dtime = current_time - last_time
		last_time = current_time

		lua_async.step(dtime)
	end
end