如何在 React Native 中下载图像期间避免“过多数量的挂起回调:501”错误?

Ale*_*min 4 async-await reactjs react-native react-redux react-native-fs

我需要在按下按钮时下载一组图像。目前,我正在使用以下方式这样做react-native-fs

const downloadImageItem = async (imgUrl, id) => {
  const path = `${RNFS.DocumentDirectoryPath}/${id}.jpg`;

  RNFS.downloadFile({
    fromUrl: imgUrl,
    toFile: path,
  });
};
Run Code Online (Sandbox Code Playgroud)
const downloadImages = async (items) => {
  for (const item of items) {
    if (item.images.length) {
      await downloadImageItem(item.images[0].thumb, item.images[0].id);
    }
  }
  return Promise.resolve();
};
Run Code Online (Sandbox Code Playgroud)

从我的减速器中为 3 种类型的项目调用函数:

await downloadImages(items_one);
await downloadImages(items_two);
await downloadImages(items_three);
Run Code Online (Sandbox Code Playgroud)

我的问题是有时我会收到一条错误消息: 过多的待处理回调数:501

有没有更好的方法来做同样的事情,这样错误就不会出现?

eme*_*nto 6

2019 年 12 月在存储库上打开了一个类似的问题react-native,它仍未关闭或解决,但我认为您可能会发现此评论很有用。

那里描述的问题是排队太多的Promise呼叫可能会出现问题。评论者建议使用来自bluebird库的Promise实用程序函数Promise.map(),该函数具有开箱即用的并发控制功能。