0

I'm not getting the return value I'm expecting from an async function when I try to save the return value in a variable. When I step into my function and inspect the return value with the debugger statement (an array of numbers representing categories i.e. [123, 423, 874, 999, 234, 452]) it is what I expect it to be.

I am using the run() function as that I'm using as a wrapper for when I call the getRandomCategories() function. When I console.log(res) it is an array of ids (this is what I'm expecting)

But when I try to save the return value in a variable (const categoriesArray = run()) I'm expecting an array of ids so I can use the array for another function instead I'm getting a Promise with a pending state. What am I missing?

Here's my code:

async function getData(endpoint, query, value) {
  const res = await axios.get(
    `http://jservice.io/api/${endpoint}?&${query}=${value}`
  );
  return res;
}

// createa a function that will return 6 random categories
async function getRandomCategories() {
  try {
    const res = await getData('categories', 'count', 50);
    const data = res.data;
    const categories = filterCategoryData(data); // I'm filtering for category id with clues_count === 5
    const categoryIdArr = mapCategoryIds(categories); // an array of just category Ids
    const shuffledCategoryIds = shuffle(categoryIdArr);
    const apiCallCategoryArray = takeFirstXItems(shuffledCategoryIds, 6);
    return apiCallCategoryArray;
  } catch (err) {
    console.log(err);
  }
}

async function run() {
  const res = await getRandomCategories()
  // console.log(res) logs my expected output
  return res // I want it to return an array of numbers.
}

const categoriesArray = run() // I'm expecting and array of ids
console.log(categoriesArray) // Why am I not gettng an array of ids in     
//my variable? Instead I get a Promise: state <pending>
tedico
  • 67
  • 5
  • 3
    your function is `async` and therefor it returns a promise. `run().then(res => console.log(res))` – messerbill Aug 07 '20 at 00:57
  • Although you are using async await in run(), getRandomCategories(), and getData(), when you invoke run(), it is still a async operation in the global execution context. And at the moment, you can't do as async await in the global space AFAIK – codemax Aug 07 '20 at 01:54

1 Answers1

1

Since run() returns a Promise as the questioner discovered, either await for the resolved value or attach a then handler as follow.

run().then(categoriesArray => console.log(categoriesArray));
TimTIM Wong
  • 788
  • 5
  • 16
  • please add some explanation to your answer, only a snippet does not make it a good answer. – Prafulla Kumar Sahu Aug 07 '20 at 07:10
  • @PrafullaKumarSahu I tried to be the first to answer, and then edit. But the question is now closed due to duplicate. – TimTIM Wong Aug 07 '20 at 07:12
  • 1
    does not matter if it is closed, still if you have answered make it better, it will help someone in future, if the original question has same answer as your, you can also delete it. – Prafulla Kumar Sahu Aug 07 '20 at 07:13
  • 1
    @PrafullaKumarSahu I agree, that some explanation should be better...BUT: my best rated answers are the ones with nearly no explanation at all :D i guess a lot of ppl just wanna see the code to copy and paste it and modify it depending on their needs :D – messerbill Aug 07 '20 at 15:04
  • @messerbill https://stackoverflow.com/help/how-to-answer I hope if you are reviewing posts, you might have gone through this guidelines. – Prafulla Kumar Sahu Aug 07 '20 at 15:22
  • @PrafullaKumarSahu i guess the community can decide by their own if they either up- or downvote questions and answers. Sometimes too much explanation confuses ppl more than it does actually help – messerbill Aug 07 '20 at 15:23
  • @messerbill I assume guidelines is most people's view, if you think its need to be changed, rasie this point in meta and we will see, how people are thinking about it. – Prafulla Kumar Sahu Aug 07 '20 at 15:25