# async.thread (Shared)

> Runs a function in a new async thread, right away. The call returns when the function ends or first pauses, as with `async.wait`.

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Function
- **Side**: Shared
- **Category**: Timers and async
- **URL**: https://wiki.mymp.gg/en/async.thread

## Syntax

```lua
any async.thread(function func [, function callback = nil])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `func` | `function` | The function the thread runs. |
| `callback` | `function` | Called with the values `func` returned, when it ends without an error. *(default: `nil`)* |

## Returns

The thread (a Lua coroutine), to use with `async.cancel` and `async.status`.

An error inside the thread is logged with its stack trace. A resource's threads end when it stops.

## Example

```lua
async.thread(function()
    for i = 3, 1, -1 do
        outputDebugString("starting in " .. i)
        async.wait(1000)
    end
    return "go"
end, function(result)
    outputDebugString(result)
end)
```

## See also

- [async.wait (Shared)](https://wiki.mymp.gg/en/async.wait.md): Pauses the current async thread for ms milliseconds. The rest of the game keeps running.
- [async.mutex (Shared)](https://wiki.mymp.gg/en/async.mutex.md): Runs a function in a new async thread, as async.thread does, but does not start while another thread started from the same line of code, with the same key, is still running.
- [async.cancel (Shared)](https://wiki.mymp.gg/en/async.cancel.md): Stops an async thread of this resource. A paused thread ends at once; called from the thread itself, it stops right there.
- [async.status (Shared)](https://wiki.mymp.gg/en/async.status.md): Tells what state an async thread of this resource is in.
