# async.map (Compartilhado)

> Chama `handler(valor, chave)` para cada entrada da tabela, na ordem de `pairs`, e junta numa lista os resultados que não são `nil`. O `handler` pode pausar com `async.wait`.

- **Plataforma**: MY Multiplayer (MYMP)
- **Tipo**: Função Lua
- **Lado**: Compartilhado
- **Categoria**: Tempo e assíncrono
- **URL**: https://wiki.mymp.gg/pt/async.map

## Sintaxe

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

## Parâmetros

| Nome | Tipo | Descrição |
| --- | --- | --- |
| `list` | `table` | Tabela a percorrer. |
| `handler` | `function` | Função chamada com o valor e a chave de cada entrada. |
| `callback` | `function` | Chamada com a lista de resultados quando tudo termina. *(padrão: `nil`)* |

## Retorno

Dentro de uma thread async e sem `callback`, a lista de resultados. Senão, roda numa thread nova e devolve `true`.

## Exemplo

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

### Atenção

Fora de uma thread async e sem `callback`, os resultados se perdem.

## Veja também

- [async.all (Compartilhado)](https://wiki.mymp.gg/pt/async.all.md): Roda cada função da lista na sua própria thread, todas ao mesmo tempo, e faz a thread atual esperar até todas terminarem.
- [async.thread (Compartilhado)](https://wiki.mymp.gg/pt/async.thread.md): Roda uma função numa thread async nova, na hora. A chamada retorna quando a função termina ou faz a primeira pausa, como async.wait.
