async/await
Programacao assincrona em TLPP. Permite chamadas HTTP/IO sem bloquear thread principal. Sintaxe inspirada em JS/TS.
Assinatura: async function Foo() : Promise { ... } // await Foo()
Retorna: Promise
async/await sao palavras-chave de TLPP para programacao assincrona. Permitem chamar IO (HTTP, banco) sem bloquear o thread, similar a JS/TS modernos.
#include "tlpp-core.th"
namespace custom.api
async function BuscaCliente(cId)
Local oHttp := FwHttpClient():New()
Local oRes := await oHttp:Get("https://api.ext.com/clientes/" + cId)
Return oRes:GetBody()
User Function Test()
Local oCli := await custom.api.BuscaCliente("123")
ConOut(oCli)
Return
Paralelo
// Sequencial: 2x 500ms = 1000ms
oA := await BuscaCliente("1")
oB := await BuscaCliente("2")
// Paralelo: max(500ms, 500ms) = 500ms
oPa := BuscaCliente("1") // sem await — Promise solta
oPb := BuscaCliente("2")
oA := await oPa
oB := await oPb
Pegadinhas
- async so dentro de async — User Function comum nao pode usar await sem ser async.
- Esquecer await deixa Promise pendurada (vaza recurso).
- Excecao propaga via Promise — use try/catch ao redor do await.
- Disponivel desde Protheus 12.1.220x — verifique versao do AppServer.