I'm trying to store in a variable ('randomColor') a random color from fetched colors, so that I can use it in my application. Whenever I log it in the console, it shows it is undefined. Logging the color directly from the fetch:
.then(color => console.log(color))
has been successful, since the console shows a random color.
I also tried fetching all the colors and getting a random color from them, but the same issue.
var randomColor;
globals = {
currentColor: randomColor
};
fetch('https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json')
.then(res => res.json())
.then(obj => Object.keys(obj))
.then(keys => keys[Math.floor(Math.random() * keys.length)])
.then(color => randomColor = color);
What to do?