promise
作者:互联网
a.then返回的是新的promise,第一个参数是resolved状态的回调函数,第二个参数是rejected状态的回调函数。
b.catch返回的也是新的promise,如果异步发生错误变成rejected状态,就会调用catch。注意:如果前面的then已经对reject做处理(then的第二个参数做了处理)了,catch就不会再处理
举个例子:
new Promise((resolved, rejected) => { throw new Error('promise error'); rejected(); }).then(() => { console.log('then resolved') }, (error) => { console.log('then rejected'); console.log(error); }).catch((error) => { console.log('catch rejected');
console.log(error); }) // then rejected // Error: promise error
//由此可见,then对rejected做了处理后,catch将不再处理
但是,then的回调函数中出现的错误也会被catch捕获,如下:
new Promise((resolved, rejected) => { throw new Error('promise error'); rejected(); }).then(() => { console.log('then resolved') }, (error) => { console.log('then rejected'); console.log(error); throw new Error('rejected then error'); }).catch((error) => { console.log('catch rejected'); console.log(error); }) // then rejected // Error: promise error // catch rejected // Error: rejected then error
标签:console,log,rejected,promise,error,catch 来源: https://www.cnblogs.com/superil/p/15720720.html