gpt4 book ai didi

javascript - 如何等待 firebase 图片上传

转载 作者:行者123 更新时间:2023-11-30 15:34:10 25 4
gpt4 key购买 nike

我正在尝试将多张图片上传到 firebase,但我需要返回的 url 才能继续。有没有什么办法可以在继续之前等待返回?

我的代码是:

data.forEach((element) => {
const sessionId = new Date().getTime();
Blob.build(RNFetchBlob.wrap(element.path), { type : 'image/jpeg' })
.then((blob) => {firebase.storage()
.ref('images')
.child(`${sessionId}`)
.put(blob, { contentType : 'image/png' })
.then((snapshot) => {
element.image = snapshot.metadata.downloadURLs[0];
})
});
});

Parse.Cloud.run('doSomething', {
data : data,
}).then(res => {
dispatch({
type: 'WE_DID_SOMETHING',
});
});

我想上传多张图片,所以在调用 Parse 之前我会多次调用 firebase。在继续之前,如何确保从 firebase 调用返回 url?

如有任何帮助,我们将不胜感激。

最佳答案

我相信您正在寻找的是Promise.all():

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Promise.all(array) 只有在 array 中的所有 promise 都已解决后才会解决。让您的 forEach 在每次迭代时创建一个 promise 对象(而不是使用 .then() 单独处理每个解析)并将其推送到数组,然后运行 ​​Promise.all(array)data.forEach() 调用之后。

关于javascript - 如何等待 firebase 图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41820893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com