API coroutine.yield
Jump to navigation
Jump to search
Yields execution until resumed.
[...] = coroutine.yield([...])
Arguments
- ...
- Variable arguments - Parameters returned to the previous coroutine.resume() which had begun or resumed this coroutine.
Returns
- ...
- Variable arguments - Parameters supplied by the next coroutine.resume() which resumes this coroutine.
Example
Yields every 1ms and resumes on the next frame until completion.
local function workload(stopTime)
for i=1, 1000000 do
-- wasting time
local x = i * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9
if GetTimePreciseSec() >= stopTime then
stopTime = coroutine.yield(1000000 - i) -- passes the number of iterations left, receives a new stopTime
end
end
end
local co = coroutine.create(workload)
local ticker
ticker = C_Timer.NewTicker(0, function()
local success, remaining = coroutine.resume(co, GetTimePreciseSec() + 0.001)
if success and remaining then
print("There are " .. remaining .. " iterations left.")
else
ticker:Cancel()
print("Reached the end!")
end
end)
External links
- Roberto lerusalimschy 2003, Programming in Lua, Chapter 9.1: Coroutine Basics.