javascript - React.js,在所有 getDownloadURL 函数完成后打印一些东西
问题描述
我正在尝试从 Firebase 获取所有 URL。所有getDownloadURL
完成后我需要打印一些东西。我试图在.then(function()){}
之后添加storageRef.listAll()
,但它不起作用。有人能帮我吗?太感谢了!
getAllURL = product => {
// Get all the images from the firebase
var storage = firebase.storage();
var that = this;
const storageRef = storage.ref(`image/${product}`)
storageRef.listAll().then(function(result) {
result.items.forEach(function(imageRef) {
imageRef.getDownloadURL().then(function(url) {
let a = 'a'
that.state.temp3.push(a)
console.log("789")
}).catch(function(error) {});
console.log("123")
})
console.log("456")
})
}
解决方案
我声明了一个名为的变量promises
并将所有承诺分配给它。在那之后,我曾经Promise.all
等待所有的承诺得到解决。
getAllURL = product => {
// Get all the images from the firebase
var storage = firebase.storage();
var that = this;
const storageRef = storage.ref(`image/${product}`);
storageRef.listAll().then(function(result) {
const promises = result.items.map(function(imageRef) {
return imageRef
.getDownloadURL()
.then(function(url) {
let a = "a";
that.state.temp3.push(a);
console.log("789");
})
.catch(function(error) {});
});
Promise.all(promises)
.then((results) => console.log('Promises resolved', results))
});
};
推荐阅读
- matlab - Matlabs“splitapply”用于具有多个参数的函数
- python - 过期时间后将模型字段值更改为 True
- gradle - 如何在本地 kotlinc 上运行远程调试
- .net - 使用 `dotnet new webapi` 命令创建项目时,我在 Web Api 项目中没有 **App_Start** 文件夹
- julia - 保存数组,HDF5,Julia
- python - Txt 输出不是我所期望的
- python - 尝试清除 Tkinter 条目小部件时出错
- android - 可绘制中的TextView设置attr / selectableItemBackground
- regex - 从遵循 bash 脚本中的模式的单行中删除主机名
- python - 如何使用 Python 3 在某些 html 标签之间提取文本?