解决方法:“ 10000毫秒后无法从port()连接到(URL)”

时间:2019-05-29 14:18:13

标签: javascript reactjs react-native promise async-await

我正在构建一个React Native应用程序,但是在下载一些图像时遇到了问题。问题是,在成功下载了几张图像后,我遇到错误并说

  

[未处理的承诺拒绝:错误:10000毫秒后从/10.0.3.15连接到javatrade.maniatest.win/ip-adress失败。

我认为问题出在我的代码中,我尝试过记录几次,但问题仍然存在。

constructor(props) {
    super(props);
    this.state = {
        downloadImages: [],
        arrLength: 0,
        progress: 0
    };
}

componentWillMount = async() => {
    axios.post('apilink')
        .then(async (response) => {
            await this.setState({ downloadImages: response.data.images, arrLength: response.data.images.length });
        })
        .finally(async() => {
            this.startDownloading();
        })
}

startDownloading = async() => {
    for(let i = 0; i < this.state.downloadImages.length; i++){
        await this.processArray(this.state.downloadImages[i])
    }

    console.log('Finished');
}

processArray = async(el) => {
    const { progress, arrLength } = this.state;
    this.setState({ progress: progress + (1/arrLength) });
    await new Promise(async(resolve) => {
        const name = el.split('/').pop();
        const imageInfo = await FileSystem.getInfoAsync(`${FileSystem.documentDirectory}allImages/files/${name}`);
        if(imageInfo.exists) {
            console.log('Already exists');
            return resolve();
        } else {
            await FileSystem.downloadAsync(el, `${FileSystem.documentDirectory}allImages/files/${name}`);
            console.log('Finished downloading');
            return resolve();
        }
    })
}

因此,我希望从该组件中下载API提供的链接中的图像(其中约有50个)。有时我下载3,然后发生Promise错误,有时我下载12,然后发生Promise错误,我不确定自己在做什么错?

这是console.log中错误的图片链接。

https://i.imgur.com/a8XxX99.png

0 个答案:

没有答案