阮一峰的ECMAScript6入门Promise对象第四小节提到:promise抛出一个错误,就被catch方法指定的回调函数捕获。constpromise=newPromise(function(resolve,reject){thrownewError('test');});promise.catch(function(error){console.log(error);});等同于://写法一constpromise=newPromise(function(resolve,reject){try{thrownewError('test');}catch(e){reject(e);}});promise.catch(function(error){console.log(error);});//写法二constpromise=newPromise(function(resolve,reject){reject(newError('test'));});promise.catch(function(error){console.log(error);});请问:为什么如下的形式,catch无法捕获错误constpromise=newPromise(function(resolve,reject){setTimeout(function(){thrownewError('test')},0)});promise.catch(function(error){console.log(error)});但如果改成如下两种形式就可以。//改写方法1constpromise=newPromise(function(resolve,reject){setTimeout(function(){try{thrownewError('test')}catch(e){reject(e)}},0)});//改写方法2constpromise=newPromise(function(resolve,reject){setTimeout(function(){reject(newError('test'));},0)});
慕容3067478
相关分类