promise 错误处理 catch

js yekong 611℃

Promise 会自动捕获内部异常,并交给 rejected响应函数处理。

console.log('here we go');
new Promise(resolve => {
    setTimeout(() => {
        throw new Error('bye');
    }, 2000);
})
    .then(value => {
        console.log(value + ' world');
    })
    .catch(error => {
        console.log('Error: error. message')
    })

错误处理的两种做法:

reject('错误信息').then(null, message => {
})

throw new Error('错误信息').catch(mesSage => {})
喜欢 (0)