The coroutine module is a native module of lua. For use, see: lua official manual
::: tip API
coroutine.create(f: <function>)
:::
| Parameter | Description |
|---|---|
| f | Function to create coroutine |
Creates a new coroutine with function f. Returns a thread object that represents the coroutine.
local co = coroutine.create(function()
print("Hello from coroutine")
end)
::: tip API
coroutine.resume(co: <thread>, ...)
:::
| Parameter | Description |
|---|---|
| co | Coroutine thread |
| ... | Arguments to pass to the coroutine |
Starts or continues the execution of coroutine co. Returns true if the coroutine execution is successful, false otherwise.
local success, result = coroutine.resume(co, "arg1", "arg2")
::: tip API
coroutine.yield(...)
:::
| Parameter | Description |
|---|---|
| ... | Values to return to the caller |
Suspends the execution of the calling coroutine and returns values to the caller.
coroutine.yield("value1", "value2")
::: tip API
coroutine.status(co: <thread>)
:::
| Parameter | Description |
|---|---|
| co | Coroutine thread |
Returns the status of coroutine co: "suspended", "running", "normal", or "dead".
local status = coroutine.status(co)
::: tip API
coroutine.wrap(f: <function>)
:::
| Parameter | Description |
|---|---|
| f | Function to create coroutine |
Creates a new coroutine with function f and returns a function that resumes the coroutine each time it is called.
local func = coroutine.wrap(function()
return "Hello"
end)
local result = func()
::: tip API
coroutine.running()
:::
No parameters required for this function.
Returns the running coroutine, or nil when called by the main thread.
local co = coroutine.running()