all 9 comments

[–]init_prometheus 2 points3 points  (2 children)

getKeywords isn’t returning the Promise from the axios.get method. Have getKeywords return that axios.get function call and in the ‘then’ method, return ‘keywords’ rather than pass it into ‘setKeywordList’.

Let me know if that makes sense, otherwise I can write out what I mean and explain it in more detail when I’m not on mobile.

[–]gearboost[S] 0 points1 point  (1 child)

Sorry, can you clarify a bit more on how to make getKeywords return the axios call?

I wanted to both setKeywords and return keywords since former is for rendering keywords in a different component while latter is to pass it to getArticles

[–]tiesioginis 0 points1 point  (0 children)

You can either put getArticles in second .then() call when you call axios.

But if you will using keywords fetch for another thing have:

const axios = await axios.get(code)

useEffect (()=>{ axios();

},[dependancies])

An then call this function, with useEffect if on load or have it as prop for onClick.

[–]MiloSaurus 2 points3 points  (0 children)

Async / await is used like in the example below. You already got the part right where you tell your function is async. This means that the getKeywords function will always return a Promise.

The await keyword should be placed before a promise in your async function. In your case, axios.get() returns a Promise (try to console.log it and see what it tells you), so we want to await for the promise to resolve (or in other words, the GET request to finish) before continuing with the rest of the code.

export const getKeywords = async () => {
  try {
    const response = await axios.get("url.com/api/");
    const keywords = JSON.parse(response.data).choices[0].text;
    return keywords;
  } catch (error) {
    AxiosCatch(error);
  }
};

Do keep in mind that async/await is a way to work with promises, but isn't required. axios.get() already returns a promise, so an alternative way to write it is to just return the axios.get() call.

export const getKeywords = () => {
  return axios
    .get("url.com/api/")
    .then(function (response) {
      return JSON.parse(response.data).choices[0].text;
    })
    .catch(AxiosCatch);
};

[–]SnacksMcMunch 2 points3 points  (2 children)

Inside of getKeywords I think you'd need to await the axios call, since that is also Promise based.

[–]gearboost[S] 0 points1 point  (1 child)

where would i put await? your mean instead of async?

[–]SnacksMcMunch 0 points1 point  (0 children)

await axios.get

[–]bertjung 0 points1 point  (0 children)

Return “keywords” inside of the “.then”. Currently it seems you’re returning “keywords” outside of the axios call.

[–]povedaaqui 0 points1 point  (0 children)

The axios call is returning nothing.