promise reject

2020-03-19  本文已影响0人  空天啸鹤

1.promise((resolve, reject) =>{}), 逻辑中必须调用resolve 或 reject来返回数据或继续执行外层逻辑,否则无法执行;

2.使用async await 组合是,需要做catch,否则出现uncatch err 导致后续代码无法执行;

// postSignInAPI({ id, type })
      //   .then(res => {
      //     console.log('-signin-ok:', type, id, res)
      //   })
      //   .catch(res => {
      //     console.log('-signin-errr:', type, id, res)
      //   })
      console.log('-signin-2-', type, id)
      const res = await postSignInAPI({ id, type }).catch(e => {
        console.log('-signin-errr:', type, id, e)
      })
      console.log('-signin--', type, id, res)
      if (res) {
      }

3.return new promise((resolve, reject) =>{}).catch(e =>{}), 逻辑中reject被catch住的话,异常将无法抛出,改进如下:

 if (config && config.dontCatchErr) {
    return new Promise(reqFun)
  }
  return new Promise(reqFun).catch(e => {
    console.error('catch: ', e)
  })
上一篇下一篇

猜你喜欢

热点阅读