Fetching Data from API for Dummies (JavaScript).

Subscribe to my newsletter and never miss my upcoming articles

Fetch API + for of + async/await

we would not be using Promise.all and Promises chaining

const urls = [
  'https://jsonplaceholder.typicode.com/users',
  'https://jsonplaceholder.typicode.com/posts',
  'https://jsonplaceholder.typicode.com/albums',
];
async function getDatafromAPI(){
  const dataInArray = urls.map(url=>fetch(url));
  for await (let data of dataInArray){
        const dataConvetedtoObject=await data.json()
        console.log(dataConvetedtoObject)
  }
}

getDatafromAPI()

Result

Screenshot (139).png

Thankyou for Reading,

Comments (1)

Catalin's Tech's photo

I've just heard of jsonplaceholder.typicode.com now. Thank you!