Skip to content
Advertisement

problems with an array awaiting for a function that reads from firestore

I’m trying to build a method which reads from firestore an array of elements (object):

I have a service which retrieves the data from firestore, first it gets an array of document references

var data = snapshot.get(‘elements’);

and then it gets all the objects:

JavaScript

}

Then in a component I have an async method which calls the service, and tries to push into another array all the names from each object in the first array:

JavaScript

However when I look to the console, the first array (array) gives me indeed an array of objects, but the other array (names) is empty. I used the method get to retrieve the data because I don’t want to listen to it, I might need the value just once.

Advertisement

Answer

Personally I find the async/await syntax infinitely more elegant and easier to deal with than a good old .then() callback hell :

JavaScript

If you use for...of, all calls will be made one after the other, in order. If you use await Promise.all(), all calls will be made and awaited simultaneously, which is faster but recommended only if you have a small number of calls to make (otherwise this could overload the server you’re calling, or even be considered as a DDoS attack.)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement