1/*
2ORIGINAL CODE
3*/
4
5videoUrls=()=>{
6 let i=0;
7 let urllist=[]
8 for(i;i< this.state.data.length;i++){
9 fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${this.state.data[i].name}&key=xxxxxxxxxxx0`)
10 .then(response=> {
11 return response.json()
12 })
13 .then(data=>{
14 return(urllist.push(data.items[0]))
15 })
16 }
17 console.log({urllist})
18}
19
20/*
21Your for loop does not iterate asynchronously but you can get around
22this by putting your for loop inside an async function Asynchronous
23Process inside a javascript for loop and awaiting the result of the
24asynchronous operations
25*/
26
27/*
28New code
29*/
30
31videoUrls = async () => {
32 let i=0;
33 let urllist=[]
34 for(i;i< this.state.data.length;i++){
35 const response = await fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${this.state.data[i].name}&key=xxxxxxxxxxx0`)
36 const json = await response.json()
37 urllist.push(json.items[0])
38 console.log({urllist})
39 }
40 }
41