问题描述
我需要在按下按钮时下载一组图像.目前,我正在使用 react-native-fs
这样做:
I need to download a collection of images on button press. Currently, I'm doing it this way using react-native-fs
:
const downloadImageItem = async (imgUrl, id) => {
const path = `${RNFS.DocumentDirectoryPath}/${id}.jpg`;
RNFS.downloadFile({
fromUrl: imgUrl,
toFile: path,
});
};
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();
};
从我的减速器中为 3 种类型的项目调用函数:
Calling the function from my reducer for 3 types of items:
await downloadImages(items_one);
await downloadImages(items_two);
await downloadImages(items_three);
我的问题是有时我会收到一条错误消息,内容为:待处理的回调数量过多:501
My issue is that sometimes I receive an error message that says:Excessive number of pending callbacks: 501
有没有更好的方法来做同样的事情,这样错误就不会出现?
Is there a better way of doing the same thing, so that the error does not appear?
推荐答案
一个类似的issue 于 2019 年 12 月在 react-native
存储库中打开,它仍然没有关闭或解决,但我想你可能会发现这个 评论 很有用.
A similar issue was opened on the react-native
repository in December 2019, it still hasn't been closed or addressed but I think you may find this comment useful.
那里描述的问题是排队太多 Promise
调用可能会出现问题.评论者建议使用名为 Promise.map()
的 Promise
实用程序函数,该函数具有开箱即用的并发控制,来自 bluebird 库.
The problem being described there is that queueing too many Promise
calls can be problematic. The commenter suggested using a Promise
utility function called Promise.map()
which has concurrency control out of the box, from the bluebird library.
这篇关于如何避免“过多的挂起回调:501"在 React Native 中下载图像时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!