Reactjs 如何避免;挂起的回调次数过多:501“;在React Native中下载图像时出错?
我需要下载一个按钮按下图像集合。目前,我使用Reactjs 如何避免;挂起的回调次数过多:501“;在React Native中下载图像时出错?,reactjs,react-native,async-await,react-redux,react-native-fs,Reactjs,React Native,Async Await,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, }); }; 从my reducer为3种类型的项调用函数: a
react native fs
这样做:
const downloadImageItem = async (imgUrl, id) => {
const path = `${RNFS.DocumentDirectoryPath}/${id}.jpg`;
RNFS.downloadFile({
fromUrl: imgUrl,
toFile: path,
});
};
从my reducer为3种类型的项调用函数:
await downloadImages(items_one);
await downloadImages(items_two);
await downloadImages(items_three);
我的问题是,有时我会收到一条错误消息,上面说:
挂起的回调次数过多:501
有没有更好的方法来做同样的事情,这样就不会出现错误?2019年12月,在react native
存储库上打开了一个类似的文件,它仍然没有被关闭或解决,但我想您可能会发现这很有用
这里描述的问题是,排队等待太多的
Promise
调用可能会有问题。评论者建议使用库中名为Promise.map()
的Promise实用程序函数,该函数具有即时可用的并发控制。尝试使用for wait,而不是在for中使用wait
for await (variable of iterable) {
sentence
}
我不确定我在这里做错了什么,但我一直收到TypeError:Promise.map不是函数
错误消息。尽管我从“蓝鸟”那里承诺导入了import*
。并将下载函数重构如下:const downloadImages=async(items)=>{await Promise.map(items,(item)=>downloadImageItem(item),{concurrency:200,});return Promise.resolve();}
包安装正确吗?npm或纱线。下面是Promise.map()
函数的用法。尝试使用它,但无法使其工作。如果可以,请查看:
for await (variable of iterable) {
sentence
}
const downloadImages = async (items) => {
for await(const item of items) {
if (item.images.length) {
downloadImageItem(item.images[0].thumb, item.images[0].id);
}
}
return Promise.resolve();
};