I'm testing a basic usage of async/await in Javascript and trying to reach a conclusion. So, based in this test I made, it is impossible to make the code completely stop and wait for the response ? If i want to make use o the data that come from json() I have to use only inside then() method ?
async function fetchData(str = ''){
const response = await fetch(`https://rickandmortyapi.com/api/${str}`)
const data = await response.json()
console.log(data)
return data
}
let res = null
fetchData('character').then(e => {
console.log(e)
res = e
})
console.log(`number of characters is ${res}`)
console output is:
index.js:16 -> number of characters is null
index.js:5 -> {info: {…}, results: Array(20)}
index.js:12 -> {info: {…}, results: Array(20)}
What would be a correct way to print the information using only async/await ?