这个封装请求既执行了then又执行了catch怎么回事?

会同时输出111和222

api.common_post('test',{
  content: this.data.content
}).then(res=>{
  console.log(111)
}).catch(fail=>{
  console.log(222)
})



const http = ({ url = '',header, param = {}, method} = {}) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      data: param,
      header: header||{
        'content-type': 'application/json'
      },
      method,
      complete: (res) => {
        resolve(res.data)
      },
      fail: error=>{
        resolve(error)
      }
    })
  })
}
阅读 1.7k
2 个回答

应该使用 success, 而不是 complete
complete 是指请求后的执行的函数,所以如果当你请求失败,completefail 都会执行

wx.request({
      xxxxxxx,
      success: (res) => {
        resolve(res.data)
      },
      fail: error=>{
        resolve(error)
      }
    })
本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题