Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
TypeScript - Return function value from the callback function

I am using the Error Handling method using the Result type, which I have posted in this question.

Using the Result type, I could easily build a chain of functions like this

Result.combine(result1, result2)
.onFailure(err => doSomethingOnFailure())
.onSuccess(val => doSomethingOnSuccess())

This flow looks good for me, but I have a problem.

function func(a, b) {
// Some code above
const result = Result.comine(aResult, bResult).onFailure(err => {
// I want to return fail result to the func here but do not know how
}).onSuccess(() => {
// Do something on success
})

// So I have to do a simple if check
if (result.failure) {
// Return fail result here
}
// Then do something on success here
}

I want to return the fail result to the parent function inside the onFailure function.

Could I somehow achieve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of writing two separate onFailure and onSuccess methods that just execute a side effect and return the Result instance itself, use a single method for both handlers that returns the result of the respective call:

handle<U>(onSuccess: (value: T) => U, onFailure: (error: string) => U): U {
  if (this.success) {
    return onSuccess(this._value);
  } else {
    return onFailure(this.error);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda