【问题标题】:How to wait for firebase image upload如何等待firebase图像上传
【发布时间】:2017-01-24 14:02:52
【问题描述】:

我正在尝试将多张图片上传到 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?

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript parse-platform react-native


    【解决方案1】:

    我相信你要找的是Promise.all()

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 2020-07-09
      • 2020-10-29
      • 2019-08-21
      • 2019-10-11
      • 2021-04-28
      • 2017-11-25
      • 1970-01-01
      相关资源
      最近更新 更多