RN中fetch请求的超时问题大家都是怎么处理的?



  • rt。我发现fetch没有选项去控制超时的问题。。。



  • do_fetch () {
            let header_obj = {
                    method: this.method,
                    headers: this.headers,
                }
    
            if (this.method != "GET") {header_obj.body = this.contentType=='application/json' ? JSON.stringify(this.data) : this.data}
    
            return new Promise((resolve, reject) => {
                let timeout = setTimeout(() => {reject("请求超时")}, 20000)
    
                fetch(this.get_full_url(), header_obj).then((response) => {
                    clearTimeout(timeout)
                    if (response.status == 200) {
                        return response.json()
                    } else {
                        return reject(response.status)
                    }
                }).then((responseData) => {
                    return resolve(responseData)
                }).catch((error) => {
                    return reject(error)
                })
            })
    
        }
    

登录后回复