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

111
Visualizações
How to get data from Promise function

I have a problem don't know why i can't get return data from the function on another file, here what i try

my file.js

const psm = require("../services/psm");
psm.show(12).then((res) => console.log(res));

my service.js

const axios = require("../plugins/axios").default;
const module = "psm";

exports.show = async (payload) => {
  await axios
    .get(`/${module}/${payload}`)
    .then((res) => {
      return res.data.data;
    })
    .catch((err) => {
      return Promise.reject(err.response);
    })
    .finally(() => {});
};

i get undefined return..

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

0

Problems in your code:

  • show function doesn't explicitly returns anything; as a result, promise returned by the show function is fulfilled with the value of undefined

Improvements that can be made in your code:

  1. catch and finally blocks are not needed; catch block is unnecessary because rejected promise returned as a result of catch block will need to be handled by the code that calls the show function.

    You will need the catch method or block in the calling code anyways. So, just remove the catch block and allow the calling code to catch and handle the error

  2. show function doesn't needs to be async. You can just return the result of axios.get or axios.get(...).then(...)

Final version of "show" method:

exports.show = (payload) => {
  return axios
    .get(`/${module}/${payload}`)
    .then((res) => {
      return res.data.data;
    });
}

You can call this function as:

psm.show(12)
   .then(res => console.log(res))
   .catch(error => { /* handle the error */ });

Alternate version of show function:

exports.show = (payload) => {
  return axios.get(`/${module}/${payload}`);
}

You can call this version of show function as:

psm.show(12)
   .then(res => console.log(res.data.data))
   .catch(error => { /* handle the 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