一般來說,不要在then方法里面定義 Reject 狀態的回調函數(即then的第二個參數),總是使用catch方法。
// bad promise .then(function(data) { // success }, function(err) { console.log('接口報錯'); // error }); // good promise .then(function(data) { //cb // success }) .catch(function(err) { console.log('接口或處理邏輯出錯'); // error });
上面代碼中,第二種寫法要好於第一種寫法,理由是第二種寫法可以捕獲前面then方法執行中的錯誤,也更接近同步的寫法(try/catch)。因此,建議總是使用catch方法,而不使用then方法的第二個參數。
