0

My current implementation doesn't work because the console.log for "after" is executing before the .map iteration on querySnapshop.docs has finished. In my console I see "before", "after", and then "removing..."

How do I rework this to have the correct execution order?

const uid = this.afAuth.auth.currentUser.uid;
let pollIds = polls.map(poll => poll.payload.doc.id);

console.log("before", pollIds);

var db = firebase.firestore();
db.collection('votes').where('uid', '==', uid).get({source: 'server'}).then((querySnapshot) => {
  let totalVotes = querySnapshot.docs.length;
  let alreadyVoted = querySnapshot.docs.map(async vote => {
    vote.ref.get().then(doc => {
      let pollId = doc.data().poll
      var index = pollIds.indexOf(pollId);
      if (index > -1) {
        console.log("removing...", pollIds[index]);
        pollIds.splice(index, 1);
      }

    });
  });
  console.log("after", pollIds);
});
1
  • Promise.all() the map Commented May 21, 2019 at 16:07

1 Answer 1

1

You can easily rewrite your code using async/await. It will become easier to read, to write, to maintain, plus it will log your after message as wished.

(async () => {
    console.log('before', pollIds);

    const uid = this.afAuth.auth.currentUser.uid;
    const pollIds = polls.map(poll => poll.payload.doc.id);


    const db = firebase.firestore();
    const querySnapshot = await db.collection('votes').where('uid', '==', uid).get({source: 'server'});
    const docs = querySnapshot.docs;
    const totalVotes = docs.length;

    for (const vote of docs) {
        const doc = await vote.ref.get();
        const pollId = doc.data().poll;
        const index = pollIds.indexOf(pollId);
        if (index > -1) {
            console.log('removing...', pollIds[index]);
            pollIds.splice(index, 1);
        }
    }

    console.log('after', pollIds);
})();

I have obviously not tried the actual code, so take it as inspiration.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.