I am trying to fetch json data from the unsplash api inside of a for loop but it will not sync with the loop.
async function myfunction(){
for (x=0;x<=c.length-1; x++){
await
fetch('https://api.unsplash.com/search/photos?
client_id=123;query=' +
c[x].value).then(function(response){
response.json().then(function(data) {
//out of sync with for loop
}
});
});
}
}
2
Answers
Try this way :
You’re mixing both
async/await
syntax with good oldPromise#then
(but without returning the promise to properly chain).You could try using only
async/await
: