Promise
Container de operacao assincrona em TLPP. Retorno de qualquer funcao async. Combine com await ou .Then/.Catch.
Assinatura: oP := Promise():New(bExecutor) // :Then(b), :Catch(b)
Retorna: Promise object
Promise representa "uma operacao que vai terminar no futuro" — sucesso ou falha. E o tipo de retorno padrao de funcoes async.
#include "tlpp-core.th"
User Function ProcessaTudo()
Promise():New({|resolve, reject|
Local oRes := DownloadXML()
If oRes != Nil
resolve(oRes)
Else
reject("download falhou")
EndIf
}):Then({|x|
ConOut("Sucesso: " + Len(x))
}):Catch({|e|
ConOut("Falha: " + e)
})
Return
await vs .Then
// Estilo await (mais legivel pra sequencias)
oRes := await DownloadXML()
oXml := await ParseXML(oRes)
ProcessXML(oXml)
// Estilo .Then (chain)
DownloadXML() ;
:Then({|x| ParseXML(x)}) ;
:Then({|x| ProcessXML(x)}) ;
:Catch({|e| ConOut(e)})
Pegadinhas
- Promise sem await/Then = operacao vaza/perde resultado.
- reject sem Catch gera erro nao tratado.
- Multiple .Then encadeados — cada um pode retornar Promise nova.