Why is my variable not Global ?
I have successfully fetched data from an API, and can console.log it in one function but not another. How do I make it globally accessible please?
let movieList
let currentMovie = 0
getMovies()
displayCurrentMovie()
async function getMovies() {
movieList = await fetchMovieData()
console.log(movieList) // THIS WORKS
}
function displayCurrentMovie() {
console.log(movieList) // THIS DOES NOT WORK
}
// Get the 20 most popular movies from TMDB
async function fetchMovieData() {
const API_URL = 'https://api.themoviedb.org/3/movie/popular'
const API_KEY = '3fd2be6f0c70a2a598f084ddfb75487c'
const response = await fetch(
`${API_URL}?api_key=${API_KEY}&language=en-GB`
)
const results = await response.json()
.then((data) => {
movieList = data.results
})
return movieList
} let movieList
let currentMovie = 0
getMovies()
displayCurrentMovie()
async function getMovies() {
movieList = await fetchMovieData()
console.log(movieList) // THIS WORKS
}
function displayCurrentMovie() {
console.log(movieList) // THIS DOES NOT WORK
}
// Get the 20 most popular movies from TMDB
async function fetchMovieData() {
const API_URL = 'https://api.themoviedb.org/3/movie/popular'
const API_KEY = '3fd2be6f0c70a2a598f084ddfb75487c'
const response = await fetch(
`${API_URL}?api_key=${API_KEY}&language=en-GB`
)
const results = await response.json()
.then((data) => {
movieList = data.results
})
return movieList
}