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

192
Visualizações
Javascript - Change two function then/catch style into the async/await style

I'm try to understand how .then() function can get two arguments like this.

const promise = doSomething();
const promise2 = promise.then(successCallback, failureCallback);

or

const promise2 = doSomething().then(successCallback, failureCallback);

but I want to convert two arguments .then() to async/await like this.

const result = await doSomething()
// execute successCallback if success.
// execute failureCallback if failure.

This is the website that I'm trying to learn.

Mozilla Firefox Developer : Using promise

Mozilla Firefox Developer : Promise Chaining

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

0

When you await a promise that resolves, you directly get its value:

const result = await doSomething();

When you await a promise that rejects, it throws an exception that you can either let propagate back to the caller of the current function (as a rejected promise since all async functions return a promise and async functions turn uncaught exceptions into a rejected promise) or you can catch it yourself locally with try/catch.

try {
   const result = await doSomething();
} catch(e) {
   console.log(e);
}

It is not recommended to map promises to callbacks. It's much better to just return the promise and let the caller deal with the promise directly either with .then() or await.

If you really needed to map await to successCallback and failureCallback, then you would do this:

try {
   const result = await doSomething();
   successCallback(result);
} catch(e) {
   failureCallback(e);
}

But, at that point, you may as well just use .then() since it's less code:

 doSomething().then(successCallback, failureCallback);

But, as I said earlier, you generally don't want to map promises into callbacks. It's more likely that you wrap older callback-based APIs into promises so you can use promises for all your control-flow and not mix/match models (which tends to seriously complicate good error handling when you mix models).

about 4 years ago · Juan Pablo Isaza Relatório

0

Use try...catch

try {
  const result = await doSomething()
  // success
  successCallback();
}

catch (e) {
  // error
  failureCallback();
about 4 years ago · Juan Pablo Isaza Relatório

0

You can wrap the doSomething in a try catch method to capture the success and fail like this.

try {
const result = await doSomething()
} catch (err) {
console.log(err.message)
}
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