Promise里捕捉错误的最佳实践

Promise里的同步部分不需要try catch

new Promise((resolve, reject) => {
    throw new Error('error');
    setTimeout(() => {
        
    }, 100);
}).catch(e => {
    console.log('log', e);
});;

异步部分需要try catch

new Promise((resolve, reject) => {
    
    setTimeout(() => {
        try{
            throw new Error('error');
        } catch(e) {
            reject(e);
        }
    }, 100);
}).catch(e => {
    console.log('log', e);
});;

 

posted on 2018-11-07 09:27  chen8840  阅读(1524)  评论(0编辑  收藏  举报