如何等待 Firebase 图片上传



我正在尝试将多张图片上传到 Firebase,但我需要返回的网址才能继续。有没有办法等待返回然后再继续?

我拥有的代码是:

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 调用中获取网址?

任何帮助将不胜感激。

我相信

你要找的是Promise.all()

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

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

最新更新