axios not then the second parameter, preferably with catch

一般来说,不要在then方法里面定义 Reject 状态的回调函数(即then的第二个参数),总是使用catch方法。
// Bad 
Promise 
  .then ( function (Data) {
     // Success 
  }, function (ERR) {
     the console.log ( 'error Interface');
     // error 
  }); 

// Good 
Promise 
  .then ( function (Data) { // CB 
    // Success 
  }) 
  . the catch ( function (ERR) {
      the console.log ( 'interface or processing logic error');
     // error 
  });
上面代码中,第二种写法要好于第一种写法,理由是第二种写法可以捕获前面then方法执行中的错误,也更接近同步的写法(try/catch)。因此,建议总是使用catch方法,而不使用then方法的第二个参数。

Guess you like

Origin www.cnblogs.com/yanqiong/p/11495747.html