【发布时间】:2020-08-11 19:55:39
【问题描述】:
我正在尝试获取 Firebase 存储中的所有图像。到目前为止它有效。但问题是如何将所有数据放入异步函数中的数组中,而不仅仅是一个然后将其传递给状态?
我的代码:
async componentDidMount(){
auth().signInAnonymously();
var arr = [];
var storageRef = storage().ref("/madu_mubarak/");
await storageRef.listAll().then((result)=> {
result.items.forEach((imageItem) =>{
this.displayItem(imageItem)
});
}).catch(function(error) {
console.warn("error "+error);
});
}
async displayItem(imageItem){
var arr = [];
imageItem.getDownloadURL().
then((url) => {
console.warn("ur "+url)
arr.push(url);
this.setState({
isLoad: false,
urlImage: arr
})
}).catch((error) =>{
console.warn("error "+error);
});
// return await Promise.all(arr);
}
请帮忙。 谢谢
【问题讨论】:
-
看看 Promise.all([...])。你可以给它传递一个 promise 数组,并且只有当数组中的所有 promise 都解析时它才会解析
-
@TomSlutsky 我已经看过 Promise.all。但我还是新手,所以我不明白如何实施。
标签: firebase react-native promise firebase-storage