0
votes

I have a set of firestore user id in an array object. How can i loop over the array of userid and get each user username from firestore database. How can i achieve this using async await Java Script ES8

const array =["id1","id2"]

2

2 Answers

0
votes

Maybe this will work.

const array=["id1","id2",...]//array of ids 
async function fetchData(){
  array.map(id=>{
    await fetch(<call to database using the id).then(res=>{//OK})
      .catch(err=> {not ok})
  })
}

Async await are a cleaner way to work with asynchronous functions like this.

0
votes

You can use async/await in loop as below.

const array=["id1","id2",...]


fetchData = () => {
  array.forEach(async (id) => {
    try {
      await res = fetch(id)
      // Do something with your res 
    } catch (err) {
      console.log(err)
    }
  })
}