美文网首页
React-Native Fetch 异步递归

React-Native Fetch 异步递归

作者: 米亚流年 | 来源:发表于2019-05-18 16:14 被阅读0次
return new Promise((resolve, reject) => {
                   let headers = new Headers()
                   headers.append('Content-Type', 'application/json;charset=UTF-8')
                   headers.append('Connection', 'keep-alive')
                   // 1:取出存储的token
                   storage.load({key: Login_Info.TOKEN})
                       .then(data => data.access_token)
                       .then(token => {
                           //2.拼接请求头,发送数据
                           headers.append('Cookie', 'token=' + token)
                           setTimeout(() => {
                               this.timeout_fetch(fetch(new_url, {
                                   method: 'POST',
                                   headers: headers,
                                   body: JSON.stringify(bodyParams),
                               }))
                                   .then(response => {
                                       //3 判断是否需要需要重新获取token
                                       if (response.ok) {
                                           //3.1:如果请求成功,则直接返回
                                           resolve(response.json());

                                       } else {
                                           //3.2: 如果请求被拒接,则刷新token
                                           let token_url = this.encodeURL(Config.BASE_URL + Config.API_REFRESH_TOKEN, {token: token}, suffixType)
                                           fetch(token_url, {
                                               method: 'POST',
                                               headers: {
                                                   'Connection': 'keep-alive',
                                                   'Content-Type': 'application/json',
                                               },
                                               body: JSON.stringify(""),
                                           })
                                               .then(response => {
                                                   if (response.ok) {
                                                       //3.2.1: 刷新token成功,则重新发请求
                                                       return this.post(new_url, params, bodyParams, suffixType)
                                                   } else {
                                                       //3.2.2: 刷新token失败,则直接提示
                                                       if (requestTime == 1) {
                                                           reject(response.status)
                                                           return
                                                       }
                                                       requestTime++
                                                       return this.post(new_url, params, bodyParams, suffixType)
                                                   }
                                               })
                                               .catch(err => {
                                                   reject(err)
                                               })
                                       }
                                   })
                                   .catch((error) => {
                                       reject(error);
                                   })
                                   .done()
                           }, 500);
                       })
                       .catch(err => {
                           reject(err)
                       })
               })

相关文章

网友评论

      本文标题:React-Native Fetch 异步递归

      本文链接:https://www.haomeiwen.com/subject/ykptzqtx.html