1、promise.prototytype.then():promise实例状态改变的回调函数,有两个参数:第一个是成功回调函数,第二个是失败回调函数。他会返回一个全新的promise,因此可以继续then链式调用。
2、promise.prototype.catch():失败回调函数,状态变为rejected执行。相当于promise. prototytype.then(null/undifined,rejection)
如果promise内发生了错误,但却没有定义catch(),这时运行到这一行代码会报错,但是不会影响到promise后面代码的执行
const someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// 下面一行会报错,因为x没有声明
resolve(x + 2);
});
};
someAsyncThing().then(function() {
console.log('everything is great');
});
setTimeout(() => { console.log(123) }, 2000); //依然会输出
// Uncaught (in promise) ReferenceError: x is not defined
// 123
3、promise.prototype.finally():promise实例无论状态是什么都会执行的函数。
promise
.then(resul