0

I am trying to collect a data with API with axios. But let's say I want to use this data later on the code but whenever I get out of the async code the returned data's status becomes pending. How to return data and store it to use whenever I want in code?

Here is the code:

const fetchData = async () => {
const response = await axios.get("http://www.omdbapi.com/", {
    params: {
        apikey: "a7aeae67",
        // s: `${searchBar.value}`
        s: "vam"

    }
})
}

console.log(fetchData())
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
berboş
  • 43
  • 7

1 Answers1

0

This is the famous "blue function red function" argument. Once your code is doing something async it will always need to be accessed in async context.

So - it is impossible to access response from a non-async function and you would have to access it inside async functions by awaiting it in turn.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504