I want to push data from promise to array (coursesArray) and then use the array values else where. I am using node-fetch library to query the API. Currently when i log array inside the promise, it has values(coursesOne) however when i log the array outside the promise, it is empty(coursesTwo). How do i go about implementing the ideal solution so that coursesArray is filled with data when getCoursesForSitemap()
executes
Here is what i have implemented so far
const coursesArray = [];
const getCoursesForSitemap = () => {
fetch(coursesUrl)
.then(res => res.json())
.then(json => {
json.courses.results.map(course => {
return coursesArray.push(course.slug);
});
console.log('coursesOne', coursesArray);
})
.catch(error => {
console.log(error);
});
};
getCoursesForSitemap();
console.log('coursesTwo', coursesArray);