# async.map (Shared)

> Calls `handler(value, key)` for every entry of the table, in `pairs` order, and gathers the results that are not `nil` into a list. The `handler` may pause with `async.wait`.

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

## Syntax

```lua
table|bool async.map(table list, function handler [, function callback = nil])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `list` | `table` | The table to walk. |
| `handler` | `function` | Called with each entry's value and key. |
| `callback` | `function` | Called with the list of results once everything is done. *(default: `nil`)* |

## Returns

Inside an async thread and without `callback`, the list of results. Otherwise it runs in a new thread and returns `true`.

## Example

```lua
async.map(getElementsByType("player"), function(player)
    async.yield()
    return getPlayerName(player)
end, function(names)
    outputDebugString(table.concat(names, ", "))
end)
```

### Note

Outside an async thread and without `callback`, the results are lost.

## 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.
