Reputation: 839
i made a fetch request (i turned a text into array)and i want them to pass each array element by accident in another function.
function randomSingleWord() {
fetch('http://www.randomtext.me/api/').then(res => res.json() )
.then(data => data.text_out.split(' ');
return data //random or by order single word
}
Upvotes: 0
Views: 62
Reputation: 211560
This has some asynchronous problems that await
can fix:
async function randomSingleWord() {
let data = await fetch('http://www.randomtext.me/api/');
let words = data.text_out.split(' ');
return words[Math.floor(Math.random() * words.length)];
}
Where this is now a Promise, so then
or await
applies.
Upvotes: 1