I've been studying javascript for the love of it, I'm currently learning how to use async functions and my code actually works, but I know there is always room for improving.
I'm open to any help regarding my use of async functions and how I use the return of sizeJokesArray()
in fetchJokes()
.
const jokeContainer = document.querySelector('.joke-container');
async function sizeJokesArray() {
let url = 'https://api.icndb.com/jokes/count';
let data = await (await fetch(url)).json();
return data.value;
}
async function fetchJokes() {
let url = `https://api.icndb.com/jokes/random/${length}`;
let jokesData = [];
let data = await (await fetch(url)).json();
for (jokePosition in data.value) {
jokesData.push(data.value[jokePosition].joke);
}
return jokesData;
}
sizeJokesArray().then(size => (length = size)); // return of sizeJokesArray
jokeContainer.addEventListener('click', event => {
if (event.target.value === 'Fetch') {
fetchJokes(length).then(jokesData => (jokesArray = jokesData)).then(jokesData => (console.log(jokesData)));
}
});
<div class="joke-container">
<div class="joke-text">
<p>Fetch some jokes bro!!</p>
</div>
<div class="joke-controls">
<button type="button" value="Fetch">Fetch Jokes</button>
</div>
</div>
the existing console.log()
it's not actually used in my code just used it here to log my results for the question purpose.