# async.promise (Shared)

> Runs `handler(resolve, reject)` in a new thread and makes the current thread wait until it calls `resolve` or `reject`. Meant for waiting on a function that answers through a callback.

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

## Syntax

```lua
any async.promise(function handler)
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `handler` | `function` | A function that receives `resolve` and `reject`. Only the first call of either counts. |

## Returns

The values passed to `resolve`. Only works inside an async thread. Outside one, it logs an error and returns `false`.

## Example

```lua
local function delay(ms)
    return async.promise(function(resolve)
        setTimer(resolve, ms, 1, "done")
    end)
end

async.thread(function()
    local result = delay(500)
    outputDebugString(result)
end)
```

### Note

`reject(value)`, or an error inside `handler`, becomes an error in the waiting thread. Wrap it in `pcall` to handle it.

## See also

- [async.all (Shared)](https://wiki.mymp.gg/en/async.all.md): Runs every function in the list in its own thread, all at once, and makes the current thread wait until they have all ended.
- [async.thread (Shared)](https://wiki.mymp.gg/en/async.thread.md): Runs a function in a new async thread, right away. The call returns when the function ends or first pauses, as with async.wait.
- [fetchRemote (Server)](https://wiki.mymp.gg/en/fetchRemote.md): Makes an HTTP or HTTPS request and hands the response to a function when it arrives.
