-1

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..

Md. Fazlul Hoque
  • 15,806
  • 5
  • 12
  • 32
Jazuly
  • 1,374
  • 5
  • 20
  • 43
  • 1
    `show` doesn't return anything. – tkausl Oct 09 '21 at 15:45
  • can you explain more, im not an expert.. – Jazuly Oct 09 '21 at 15:49
  • 1
    Heavily related to [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/14220323#14220323). – jfriend00 Oct 09 '21 at 15:54
  • async..await is syntax sugar for promises. It's used incorrectly here. Despite it's helpful, I'd recommend to not use it until you understand what exactly it does. That you use them together suggests that it's used accidentally without a reason. – Estus Flask Oct 09 '21 at 16:00

1 Answers1

1

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 */ });
Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • "show function doesn't needs to be async" - no `async` function *needs* to be `async`, all of them can be written with raw promises. This doesn't mean they shouldn't be used. – Estus Flask Oct 09 '21 at 16:03
  • @EstusFlask i think you misunderstood what i meant by that statement. Ofcourse every `async` function can be written using raw promises BUT I was specifically referring to `show` function. Looking at the code of this function, it is clear that it is unnecessarily made `async` because it takes no advantage of concise syntax that `async-await` syntax provides. Not to mention the unnecessary wrapping of promise created by `axios.get` by the promise created by the _async_ `show` function. – Yousaf Oct 09 '21 at 16:10
  • I agree that in this case it's easier to fix it with `return` than to rewrite the entire function to proper async..await – Estus Flask Oct 09 '21 at 16:15