resume and yield
Exchange data between caller and coroutine with resume/yield.
resume and yield is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Passing Values to Coroutine
Arguments passed to the first coroutine.resume become the function parameters of the coroutine body. Arguments passed to subsequent resumes are returned by the coroutine.yield() call that paused the coroutine.
local co = coroutine.create(function(a, b)
print("received:", a, b) -- 10 20
local c = coroutine.yield() -- pauses
print("after yield:", c) -- 30
end)
coroutine.resume(co, 10, 20) -- first call: passes a,b
coroutine.resume(co, 30) -- subsequent: passed to yieldValues from yield
Values passed to coroutine.yield(val1, val2) become the return values of the coroutine.resume() call that paused at the yield. So yield "returns" values to the caller.
local co = coroutine.create(function()
coroutine.yield(1, "hello")
coroutine.yield(2, "world")
return 3, "done"
end)
local ok, a, b = coroutine.resume(co)
print(ok, a, b) -- true 1 hello
ok, a, b = coroutine.resume(co)
print(ok, a, b) -- true 2 world
ok, a, b = coroutine.resume(co)
print(ok, a, b) -- true 3 doneBidirectional Communication
Coroutines enable true bidirectional communication: the caller passes data in via resume, the coroutine processes it and yields results back. Each resume/yield is a synchronous exchange.
local co = coroutine.create(function(x)
while true do
x = coroutine.yield(x * x) -- yield x^2, get next x
end
end)
coroutine.resume(co) -- start (x=nil initially)
local ok, sq
ok, sq = coroutine.resume(co, 3); print(sq) -- 9
ok, sq = coroutine.resume(co, 5); print(sq) -- 25
ok, sq = coroutine.resume(co, 7); print(sq) -- 49Producer-Consumer
The classic coroutine pattern: the producer yields items one at a time; the consumer resumes the producer whenever it needs the next item. Neither runs ahead of the other — perfect synchronization.
local function producer(items)
return coroutine.create(function()
for _, item in ipairs(items) do
coroutine.yield(item)
end
end)
end
local pro = producer({"apple","banana","cherry"})
-- Consumer drives the loop
while true do
local ok, item = coroutine.resume(pro)
if not ok or item == nil then break end
print("consumed:", item)
endPipeline with Coroutines
Chain multiple coroutines as a pipeline. Each stage reads from the previous and yields to the next. The final stage drives the pipeline by resuming the last stage, which cascades back through the chain.
local function source(items)
return coroutine.wrap(function()
for _, v in ipairs(items) do coroutine.yield(v) end
end)
end
local function filter(iter, pred)
return coroutine.wrap(function()
for v in iter do
if pred(v) then coroutine.yield(v) end
end
end)
end
local function map(iter, fn)
return coroutine.wrap(function()
for v in iter do coroutine.yield(fn(v)) end
end)
end
local pipeline = map(filter(source({1,2,3,4,5,6}),
function(n) return n%2==0 end),
function(n) return n*n end)
for v in pipeline do io.write(v.." ") end
print() -- 4 16 36Coroutines for Async Simulation
Without OS threads, coroutines can simulate asynchronous operations: yield while "waiting" and resume when the "result" is ready. A scheduler resumes the coroutine with the result.
local scheduler = {}
local current = nil
local function await(key)
coroutine.yield(key) -- tell scheduler what we need
return coroutine.yield() -- wait for result
end
local co = coroutine.create(function()
local data = await("fetch:users")
print("Got users:", data)
end)
-- Start coroutine
local ok, want = coroutine.resume(co)
print("Coroutine wants:", want) -- fetch:users
-- Simulate delivering the result:
coroutine.resume(co, {{name="Alice"},{name="Bob"}})yield Inside a Function
You can yield from inside any function called by a coroutine, not just the top-level function. The yield travels up through all nested calls back to the resume. This is called a "suspended call stack."
local function yieldTwice(label)
coroutine.yield(label .. ":1")
coroutine.yield(label .. ":2")
end
local co = coroutine.create(function()
yieldTwice("A")
yieldTwice("B")
end)
for i = 1, 4 do
local ok, v = coroutine.resume(co)
print(v)
end
-- A:1, A:2, B:1, B:2Coroutines Are Not Threads
Lua coroutines are not OS threads. Only one coroutine runs at a time; there is no preemption or parallelism. All coroutines share a single OS thread. For true parallelism you need multiple Lua states in separate OS threads.
-- This is NOT parallel - interleaved execution
local function task(name, n)
for i = 1, n do
print(name, i)
coroutine.yield()
end
end
local t1 = coroutine.create(function() task("A", 3) end)
local t2 = coroutine.create(function() task("B", 3) end)
-- Must explicitly schedule:
for _ = 1, 3 do
coroutine.resume(t1)
coroutine.resume(t2)
end
-- A1, B1, A2, B2, A3, B3 (interleaved, NOT parallel)Status After Resume
Check coroutine status to decide whether to resume again. A coroutine that has finished (returned from its body) is "dead" — resuming it returns false with an error. Always check status before resuming in loops.
local co = coroutine.create(function()
for i = 1, 3 do coroutine.yield(i) end
end)
while coroutine.status(co) ~= "dead" do
local ok, val = coroutine.resume(co)
if ok and val ~= nil then
print("val:", val)
end
end
-- val: 1, val: 2, val: 3Multiple Yields in One Resume
A single call to coroutine.resume runs the coroutine until the next yield or until it ends. You cannot make a coroutine yield multiple times in one resume — each yield suspends and waits for the next resume.
local co = coroutine.create(function()
local x = coroutine.yield("first")
print("got:", x)
local y = coroutine.yield("second")
print("got:", y)
end)
local _, v1 = coroutine.resume(co) -- starts, yields "first"
print("yielded:", v1) -- first
local _, v2 = coroutine.resume(co, "a") -- passes "a", yields "second"
print("yielded:", v2) -- second
coroutine.resume(co, "b") -- passes "b", coroutine endsQuick Check
What do the values passed to coroutine.yield(v1, v2) become?
Recap: resume and yield
Summary:
- First resume args → coroutine body parameters
- Subsequent resume args → returned by previous yield()
- yield args → returned by the resume() that triggered the yield
- Bidirectional: each resume/yield is an exchange
- yield from any nested function works
- Coroutines: cooperative, not parallel
Frequently asked questions
Is the “resume and yield” lesson free?
Yes — the full text of “resume and yield” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “resume and yield”?
Exchange data between caller and coroutine with resume/yield. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “resume and yield” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating Coroutines
- resume and yield
- Coroutine Status
- Producer-Consumer Pattern