Checking Coroutine Status
Part of the Logic & Flow section of Coddy's Lua journey — lesson 52 of 54.
As you work with coroutines, you'll often need to know what state a coroutine is in. Is it currently running? Is it paused and waiting to be resumed? Has it finished executing?
Lua provides the coroutine.status() function to answer these questions.
The coroutine.status() function takes a coroutine as its argument and returns a string describing its current state. There are three possible states:
| "suspended" | The coroutine is paused at a yield() and can be resumed |
| "running" | The coroutine is currently executing |
| "dead" | The coroutine has finished execution and cannot be resumed again |
Here's how you use it:
function task()
coroutine.yield()
print("Finishing")
end
local co = coroutine.create(task)
print(coroutine.status(co)) -- "suspended"
coroutine.resume(co)
print(coroutine.status(co)) -- "suspended" (paused at yield)
coroutine.resume(co)
print(coroutine.status(co)) -- "dead" (finished)Challenge
EasyWrite a function checkCoroutineLifecycle that takes resumeCount and returns a string showing the status of a coroutine at different points in its lifecycle.
Create a coroutine that yields once, then completes. Check and record its status after creation, after the first resume (when it's paused at yield), and after subsequent resumes based on resumeCount.
Logic:
- Create a function that yields the string
"paused"once, then completes - Wrap this function in a coroutine using
coroutine.create() - Check the status immediately after creation using
coroutine.status() - Resume the coroutine once and check its status again
- Resume the coroutine additional times based on
resumeCount - 1(total resumes = resumeCount) - Check the final status after all resumes
- Return a string with all three statuses in the format:
initial: [status]\\nafter first resume: [status]\\nfinal: [status]
Parameters:
resumeCount(number): Total number of times to resume the coroutine (minimum 1)
Returns: A string showing the coroutine status at three points in its lifecycle (string). Format: initial: suspended\nafter first resume: suspended\nfinal: dead
Cheat sheet
The coroutine.status() function returns the current state of a coroutine as a string.
Possible states:
"suspended"- The coroutine is paused at ayield()and can be resumed"running"- The coroutine is currently executing"dead"- The coroutine has finished execution and cannot be resumed
Usage:
coroutine.status(coroutine_object)Example:
function task()
coroutine.yield()
print("Finishing")
end
local co = coroutine.create(task)
print(coroutine.status(co)) -- "suspended"
coroutine.resume(co)
print(coroutine.status(co)) -- "suspended"
coroutine.resume(co)
print(coroutine.status(co)) -- "dead"Try it yourself
function checkCoroutineLifecycle(resumeCount)
-- Write code here
end
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Table Iteration
Iterating with pairs()Iterating with ipairs()pairs() vs. ipairs()Recap - Character Sheet2More Table Library Functions
table.concat()table construction & unpack()table.sort()Custom Sorting with FunctionsRecap - High Score Board3Advanced Function Concepts
Returning Multiple ValuesVariadic Functions (...)Functions First-Class ValuesAnonymous FunctionsWhat is a Closure?Recap - Simple Event Handler9Coroutines for Beginners
What is a Coroutine?coroutine create & resumePausing with coroutine.yield()resume & yieldChecking Coroutine StatusRecap - Number GeneratorRecap - Vector Math