I get the image URLs from firebase storage then I push every URL into a separate array to use it later when I setState,
but when I log the imgUrls outside ForEach it returns empty array!
And I tried to use Promise.all()
but not work and i can't set the state!
So what the wrong i made?
getImages = async () => {
const refList = storage().ref('/categories/Air/');
const listAll = await refList.listAll();
let imgUrls = [];
let promise = listAll.items.forEach(async itemRef => {
try {
let url = await itemRef.getDownloadURL();
imgUrls.push(url);
console.log('array-imgUrls', imgUrls);
} catch (error) {
console.log('error', error);
}
});
const imageUrls = await Promise.all(promise).then(()=>{
this.setState({URLs:imgUrls})
});
console.log("imageUrls",imageUrls);
}