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

80
Visualizações
Handling Promise object from function

I just started working with Promise objects in JS and I can't figure out something.

I need to create a function that receives a Promise object as parameter, if the promise is resolved I need to return a success object, otherwise an empty Error object. But it seems to not be returning anything.

export default function handleResponseFromAPI(promise) {
    promise.then(() => {
        console.log('Got a response from the API');
        return { status: 200, body: 'Success' };
    }, () => {
        console.log('Got a response from the API');
        return new Error();
    });
}

And I call it here:

import handleResponseFromAPI from "./2-then";

const promise = Promise.resolve();
const returnObject = handleResponseFromAPI(promise);
console.log(returnObject);

What shows up on console:

undefined
Got a response from the API

I can see that first it returns undefined and then prints the message from my function. Anyone cares to explain the process of this?

about 4 years ago · Santiago Gelvez
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