JavaScript async and await in loops

Basic async and await is not too complicated. But it gets more complicated if you want to wait for an async function in a loop like map or foreach.

The good old for loop is quite easier to handle.

1const ourArray = ["snow", "rain", "blue sky"]
2
3for (let index = 0; index < ourArray.length; index++) {
4    
5		const a = await fetch("https://source.unsplash.com/random?" + ourArray[index])
6		console.log(a)
7
8  }

← Go home