---
name: coroutine
last_updated: 2026-12-04T23:21:36Z
type: library
summary: "A function that executes alongside the main thread."
---

# coroutine

A function that executes alongside the main thread.

**Type:** library

## Description

A **coroutine** is used to perform multiple tasks at the same time from within
the same script. Such tasks might include producing values from inputs or
performing work on a subroutine when solving a larger problem. A task doesn't
even need to have a defined ending point, but it does need to define
particular times at which it **yields** (pause) to let other things be worked
on.

## Using Coroutines

A new coroutine can be created by providing a function to
[coroutine.create()](/docs/reference/engine/globals/coroutine.md). Once created, a coroutine doesn't begin running
until the first call to [coroutine.resume()](/docs/reference/engine/globals/coroutine.md) which passes the
arguments to the function. This call returns when the function either halts or
calls [coroutine.yield()](/docs/reference/engine/globals/coroutine.md) and, when this happens,
[coroutine.resume()](/docs/reference/engine/globals/coroutine.md) returns either the values returned by the
function, the values sent to [coroutine.yield()](/docs/reference/engine/globals/coroutine.md), or an error message.
If it does error, the second return value is the thrown error.

During the lifetime of the coroutine, you can call
[coroutine.status()](/docs/reference/engine/globals/coroutine.md) to inspect its status:

| Status | Meaning |
| --- | --- |
| **suspended** | The coroutine is waiting to be resumed. Coroutines begin in this state and enter it when their function calls `coroutine.yield()`. |
| **running** | The coroutine is running right now. |
| **normal** | The coroutine is awaiting the yield of another coroutine; in other words, it has resumed another coroutine. |
| **dead** | The function has halted (returned or thrown an error). The coroutine cannot be used further. |

### Wrapping Coroutines

When working with coroutines, you can also forgo the use of the coroutine
object and instead use a wrapper function. Such a wrapper function will resume
a particular coroutine when it is called and will return only the yielded
values. You can do this using [coroutine.wrap()](/docs/reference/engine/globals/coroutine.md).

The first value returned from [coroutine.resume()](/docs/reference/engine/globals/coroutine.md) describes whether a
coroutine ran without errors. However, functions returned by
[coroutine.wrap()](/docs/reference/engine/globals/coroutine.md) will not do this: instead they directly return the
values returned or passed to [coroutine.yield()](/docs/reference/engine/globals/coroutine.md), if any. Should an
error have occurred while running the coroutine function, the error is raised
on the call of the returned function.

### Producer Pattern Example

Imagine a task that produces repetitions of a word: each time it produces a
repetition, the next one will produce one more. For example, providing `Hello`
will produce `Hello`, `HelloHello`, `HelloHelloHello`, etc. To do this, you
can define `repeatThis()`, which yields the longer string each time it runs.

To run this function as a coroutine, you can use [coroutine.create()](/docs/reference/engine/globals/coroutine.md)
followed by multiple calls to [coroutine.resume()](/docs/reference/engine/globals/coroutine.md).

For this producer function, you can also use [coroutine.wrap()](/docs/reference/engine/globals/coroutine.md) to get
a function that produces values.

## Functions

### coroutine.close

**Signature:** `coroutine.close(co: thread): bool, Variant<string, void>`

Closes and puts the provided coroutine in a dead state. This function
returns `true` unless the coroutine is in an error state, in which case it
returns `false` and the error message. A coroutine that is currently
running cannot be closed. A coroutine cannot be resumed after it is
closed.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `co` | `thread` |  | The coroutine thread to close. |

**Returns:** `bool`, `Variant<string, void>` — `true` unless the coroutine being closed is in an error state. The error message, if any.

**Coroutine Close**

Demonstrates `coroutine.close`, which kills a suspended coroutine and reports
errors from failed ones.

```lua
local waiting = coroutine.create(function()
	coroutine.yield()
end)
coroutine.resume(waiting)

print(coroutine.status(waiting)) --> suspended
print(coroutine.close(waiting)) --> true
print(coroutine.status(waiting)) --> dead
print(coroutine.resume(waiting)) --> false cannot resume dead coroutine

-- Closing a coroutine that died with an error returns false plus the error
local failed = coroutine.create(function()
	error("boom", 0)
end)
coroutine.resume(failed)
print(coroutine.close(failed)) --> false boom
```

### coroutine.create

**Signature:** `coroutine.create(f: function): thread`

Creates a new coroutine with body `f`, which must be a Luau function, and
returns it as a `thread`. The returned coroutine starts in the `suspended`
state and does not begin executing until [coroutine.resume()](/docs/reference/engine/globals/coroutine.md) is
called on it. The arguments passed to the first
[coroutine.resume()](/docs/reference/engine/globals/coroutine.md) call are forwarded as the arguments to `f`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `f` | `function` |  | The Luau function to use as the coroutine body. |

**Returns:** `thread` — A new coroutine in the suspended state, ready to be resumed.

**Coroutine Create**

Demonstrates `coroutine.create`, which makes a suspended coroutine that only
starts on the first resume.

```lua
local function greet(name, punctuation)
	print("Hello, " .. name .. punctuation)
end

local greeter = coroutine.create(greet)
print(type(greeter), coroutine.status(greeter)) --> thread suspended

-- Nothing has printed yet; the arguments of the first resume go to the body
coroutine.resume(greeter, "Ada", "!") --> Hello, Ada!
print(coroutine.status(greeter)) --> dead

-- A producer that yields one more repetition of a word on each resume
local function repeatThis(word)
	local repetition = ""
	while true do
		-- Do one repetition then yield the result
		repetition = repetition .. word
		coroutine.yield(repetition)
	end
end

local repetitionCoro = coroutine.create(repeatThis)
print(coroutine.resume(repetitionCoro, "Hello")) --> true Hello
print(coroutine.resume(repetitionCoro)) --> true HelloHello
print(coroutine.resume(repetitionCoro)) --> true HelloHelloHello
```

### coroutine.finally

**Signature:** `coroutine.finally(co: thread, callback: Variant)`

Registers `callback` on coroutine `co` that will be called when `co`
reaches a terminal state. The callback is called with a status string and
a set of values:

- `callback("finished", ...results)`. The coroutine function returned. All
  result values are forwarded to the callback.
- `callback("error", err)`. The coroutine terminated with an error. The
  error value is passed as the only additional argument.
- `callback("cancelled")`. [coroutine.close()](/docs/reference/engine/globals/coroutine.md) or
  [task.cancel()](/docs/reference/engine/globals/task.md) was called on the coroutine. The coroutine did
  not complete its work. No extra values are provided.

Callbacks run at most once, in reverse registration order (LIFO).

When the coroutine reaches a terminal state during the invocation of
[coroutine.resume()](/docs/reference/engine/globals/coroutine.md) or [coroutine.close()](/docs/reference/engine/globals/coroutine.md), those
functions will return only after callbacks complete their work. Callbacks
are invoked on the calling thread and may yield. If a callback errors,
remaining callbacks in the list are not invoked and `false` status with
the callback error is returned.

Attempting to register a callback on a dead coroutine raises an error.

If a coroutine reaches a terminal state in a resume through
[coroutine.wrap()](/docs/reference/engine/globals/coroutine.md) closure and has callbacks registered, it raises
a "cannot run finalizers from a wrapped coroutine" error. A coroutine can
also reach the terminal state when it is being resumed by the engine or
closed with [task.cancel()](/docs/reference/engine/globals/task.md); in this case, callbacks are executed
on a separate thread with errors reported as script errors.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `co` | `thread` |  | The coroutine thread to observe. |
| `callback` | `Variant` |  | Function or a callable object to invoke. |

**Coroutine Finally**

Demonstrates `coroutine.finally`, which runs a callback when a coroutine
finishes, errors, or is closed.

```lua
local function observe(co, label)
	coroutine.finally(co, function(status, ...)
		print(label, status, ...)
	end)
end

local finishing = coroutine.create(function()
	return "result"
end)
observe(finishing, "finishing") --> finishing finished result
coroutine.resume(finishing)

local failing = coroutine.create(function()
	error("boom", 0)
end)
observe(failing, "failing") --> failing error boom
coroutine.resume(failing)

local cancelled = coroutine.create(function()
	coroutine.yield()
end)
coroutine.resume(cancelled)
observe(cancelled, "cancelled") --> cancelled cancelled
coroutine.close(cancelled)
```

### coroutine.isyieldable

**Signature:** `coroutine.isyieldable(): bool`

Returns `true` if the coroutine this function is called within can safely
yield. Yielding a coroutine inside metamethods or C functions is
prohibited, with the exception of `pcall` and `xpcall`.

**Returns:** `bool` — Whether or not the coroutine can safely yield at this point.

**Coroutine Is Yieldable**

Demonstrates `coroutine.isyieldable`, which reports whether the current code
may call `coroutine.yield`.

```lua
local worker = coroutine.create(function()
	print(coroutine.isyieldable()) --> true

	-- pcall is one of the few C functions that can be yielded across
	print(pcall(coroutine.isyieldable)) --> true true
end)

coroutine.resume(worker)
```

### coroutine.resume

**Signature:** `coroutine.resume(co: thread, ...: Variant): bool, Variant<Tuple, string>`

Starts or continues the execution of coroutine `co`. The first time you
resume a coroutine, it starts running its body. The values `...` are
passed as the arguments to the body function. If the coroutine has
yielded, resume restarts it; the values `...` are passed as the results
from the yield. If the coroutine runs without any errors, resume returns
true plus any values passed to yield (if the coroutine yields) or any
values returned by the body function (if the coroutine terminates). If
there is any error, resume returns false plus the error message.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `co` | `thread` |  | The coroutine thread to resume. |
| `...` | `Variant` |  | Arguments passed to the coroutine body function (on first resume) or returned from `coroutine.yield()` inside the coroutine (on subsequent resumes). |

**Returns:** `bool`, `Variant<Tuple, string>` — `true` if the coroutine ran without errors, `false` if an error
occurred. The values passed to `coroutine.yield()` or returned by the body
function on success, or the error message on failure.

**Coroutine Resume**

Demonstrates `coroutine.resume`, passing values in and out of a coroutine,
running a task that yields once, and catching errors.

```lua
local accumulator = coroutine.create(function(start)
	local total = start
	while true do
		local added = coroutine.yield(total)
		if added == nil then
			error("nothing to add", 0)
		end
		total += added
	end
end)

print(coroutine.resume(accumulator, 10)) --> true 10
print(coroutine.resume(accumulator, 5)) --> true 15

-- Errors do not propagate; resume returns false plus the error message
print(coroutine.resume(accumulator)) --> false nothing to add

-- A dead coroutine cannot be resumed again
print(coroutine.resume(accumulator)) --> false cannot resume dead coroutine

-- A task that yields once and then returns
local function task(...)
	-- This function might do some work for a bit then yield some value
	coroutine.yield("first") -- To be returned by coroutine.resume()
	-- The function continues once it is resumed again
	return "second"
end

local taskCoro = coroutine.create(task)
-- Call resume for the first time, which runs the function from the beginning
local success, result = coroutine.resume(taskCoro, "input")
print(success, result) --> true first (task called coroutine.yield())
-- Continue running the function until it yields or halts
success, result = coroutine.resume(taskCoro)
print(success, result) --> true second (task halted because it returned "second")
```

### coroutine.running

**Signature:** `coroutine.running(): thread`

Returns the currently running thread as a `thread` value. Called inside a
coroutine made by [coroutine.create()](/docs/reference/engine/globals/coroutine.md) or
[coroutine.wrap()](/docs/reference/engine/globals/coroutine.md), it returns that coroutine. Called outside any
coroutine, it returns the thread the script itself is running on, so it
never returns `nil`.

**Returns:** `thread` — The currently running thread. Outside any coroutine, this is the
thread the script itself is running on.

**Coroutine Running**

Demonstrates `coroutine.running`, which returns the thread currently
executing.

```lua
local worker = coroutine.create(function()
	local current = coroutine.running()
	print(type(current)) --> thread
	print(coroutine.status(current)) --> running
	return current
end)

local _, returned = coroutine.resume(worker)
print(returned == worker) --> true

-- Outside any coroutine it returns the thread the script itself is running on, never nil
print(type(coroutine.running())) --> thread
```

### coroutine.status

**Signature:** `coroutine.status(co: thread): string`

Returns the status of coroutine co, as a string: 'running', if the
coroutine is running (that is, it called status); 'suspended', if the
coroutine is suspended in a call to yield, or if it has not started
running yet; 'normal' if the coroutine is active but not running (that is,
it has resumed another coroutine); and 'dead' if the coroutine has
finished its body function, or if it has stopped with an error.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `co` | `thread` |  | The coroutine thread to query. |

**Returns:** `string` — One of `"running"`, `"suspended"`, `"normal"`, or `"dead"`.

**Coroutine Status**

Demonstrates `coroutine.status` across the suspended, running, normal, and
dead states.

```lua
local outer
local inner

outer = coroutine.create(function()
	inner = coroutine.create(function()
		print("outer from inner:", coroutine.status(outer)) --> outer from inner: normal
	end)
	print("outer from outer:", coroutine.status(outer)) --> outer from outer: running
	coroutine.resume(inner)
end)

print("before:", coroutine.status(outer)) --> before: suspended
coroutine.resume(outer)
print("after:", coroutine.status(outer)) --> after: dead
print("inner:", coroutine.status(inner)) --> inner: dead
```

### coroutine.wrap

**Signature:** `coroutine.wrap(f: function): function`

Creates a new coroutine, with body f. f must be a Luau function. Returns a
function that resumes the coroutine each time it is called. Any arguments
passed to the function behave as the extra arguments to resume. Returns
the same values returned by resume, except the first boolean. In case of
error, propagates the error.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `f` | `function` |  | The Luau function to use as the coroutine body. |

**Returns:** `function` — A wrapper function that resumes the coroutine each time it is called,
returning the yielded or returned values directly (without the boolean
status). Raises an error if the coroutine errors.

**Coroutine Wrap**

Demonstrates `coroutine.wrap`, which returns a function that resumes the
coroutine and raises its errors.

```lua
-- A simple coroutine body that returns a value
local function task()
	return "done"
end

local f = coroutine.wrap(task)
print(f()) --> done

-- The wrapper returns only the yielded values, without the leading boolean
local counter = coroutine.wrap(function()
	for i = 1, 3 do
		coroutine.yield(i)
	end
end)
print(counter(), counter(), counter()) --> 1 2 3

-- Unlike coroutine.resume, errors are raised at the call of the wrapper
local failing = coroutine.wrap(function()
	error("something broke", 0)
end)
print(pcall(failing)) --> false something broke

-- A producer turned into a plain function that returns each value
local function repeatThis(word)
	local repetition = ""
	while true do
		repetition = repetition .. word
		coroutine.yield(repetition)
	end
end

local repeater = coroutine.wrap(repeatThis)
print(repeater("Hello")) --> Hello
print(repeater()) --> HelloHello
print(repeater()) --> HelloHelloHello
```

### coroutine.yield

**Signature:** `coroutine.yield(...: Tuple): Tuple<Variant>`

Suspends the execution of the calling coroutine. Any arguments to yield
are passed as extra results to resume. Yielding a coroutine inside
metamethods or C functions is prohibited, with the exception of `pcall`
and `xpcall`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `...` | `Tuple` |  | Values to return as additional results from the corresponding `coroutine.resume()` call. |

**Returns:** `Tuple<Variant>` — The arguments passed to the next `coroutine.resume()` call that
continues this coroutine.

**Coroutine Yield**

Demonstrates `coroutine.yield`, which hands values to `resume` and receives
the next resume's arguments.

```lua
local conversation = coroutine.create(function()
	local reply = coroutine.yield("What is your name?")
	print("Got reply:", reply)
	local another = coroutine.yield("And your favorite color?", "(optional)")
	print("Got reply:", another)
end)

print(coroutine.resume(conversation)) --> true What is your name?
print(coroutine.resume(conversation, "Ada")) --> Got reply: Ada
-- true And your favorite color? (optional)
print(coroutine.resume(conversation, "green")) --> Got reply: green
-- true
```