I want to read data from Firebase Real-Time Database and Firebase Storage. Reading RTDB happens first. This is because the name of the files that are stored in Firebase Storage are the keys of the data in the RDTB. I have the following code:
let newReadRef = firebase.database().ref('someRef').limitToLast(3).once('value').then(snapshot => {
let promiseArray = [];
snapshot.forEach(e => {
promiseArray.push(new Promise((resolve, reject) => {
//Do all the things I need to do before trying to get data from my Firebase Storage
let filePathRef = firebase.storage().ref(e.val().key);
filePathRef.getDownloadURL().then(url => {
//Do something with the url
resolve('Promise resolved');
}).catch(e => {
console.log(e);
reject('Rejected');
});
}));
});
Promise.all(promiseArray).then(someFunction);
});
The idea is:
- Retrieve some data from my RTDB, its key will be used later to retrieve data in my Firebase Storage
- Create a new Promise for each data read in my RTDB
- For each Promise created, read data from Storage using key obtained from RTDB data. Resolve the Promise when data is read successfully. Otherwise, reject.
- After all Promises have been created, when all Promise resolves, do something using
someFunction
that will alter the data created
It seems like my code is a little too nested. Is there a better way to achieve the above?