【问题标题】:Async await with map function, promise.all not working带有地图功能的异步等待,promise.all 不起作用
【发布时间】:2019-11-05 07:57:06
【问题描述】:

我正在使用 expo 和 react-native 构建一个应用程序。

我正在尝试使用 async await 和使用 Promise.all 的 map 函数从 firebase 加载照片,但我得到的只是一系列未解决的承诺。

renderImages = async () => {
  const images = [];

  if (this.state.images.length > 0) {
    images = this.state.images.map(async (item, index) => {
      const ref = firebase.storage().ref(item);

      var image = await ref.getDownloadURL();

      return (
        <View>
          <Image source={{ uri: image }} />
        </View>
      );
    });

    const imageArray = await Promise.all(images);

    return imageArray || [];
  }

  return (
    <View>
      <Text>STATE.IMAGES.LENGTH is 0</Text>
    </View>
  );
};

我得到的只是

Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
}

【问题讨论】:

  • 这看起来不像一个promise数组,它看起来像一个promise。是的,renderImages 是一个async function,所以它当然会返回一个承诺。一切似乎都在工作。您能否向我们展示您在哪里调用该函数的代码,以及您在哪里“取回”该结果?

标签: react-native promise expo


【解决方案1】:

好的,所以我不知道 async/await 总是返回 promise

我通过添加这个来解决问题

onRenderImages() {
  this.renderImages().then(data => {
     this.setState({ 
         displayImages: data
     })
  })
}

【讨论】:

  • 啊,怀疑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2018-02-07
  • 2019-10-10
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多