es6 --- Promise.catch
生活随笔
收集整理的這篇文章主要介紹了
es6 --- Promise.catch
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Promise.prototype.catch(): 是.then(null, rejection)的別名,用于指定發(fā)生錯(cuò)誤時(shí)的回調(diào)函數(shù)
p.then( (val) -> console.log('fulfilled:', val)).catch( (err) => console.log('rejected', err));// 等同于 p.then( (val) => console.log('fulfilled:', val)).then(null, (err) => console.log("rejected:", err));// catch方法可以捕獲then方法中拋出的錯(cuò)誤 var promise = new Promise(function (resolve, reject) {throw nuw Error('test'); }); promise.catch(function (error) {console.log(error); }); // 如果Promise狀態(tài)已經(jīng)變成Resolved, 再拋出錯(cuò)誤是無(wú)效的. var promise = new Promise(function (resolve, reject) {resolve('ok');throw new Error('test from promise'); }); promise.then(function (value) { console.log(value) }).catch(function (error) { console.log(error) }); // 如果沒(méi)有使用catch方法指定錯(cuò)誤處理的回調(diào)函數(shù),Promise對(duì)象拋出的錯(cuò)誤不會(huì)傳遞到外層代碼 var someAsyncThing = function() {return new Promise (function (resolve, reject) {resolve(x + 2);}); };someAsyncThing().then(function() {console.log('everything is great'); });// 注:resolve(x + 2) 會(huì)報(bào)錯(cuò),x未定義, 控制臺(tái)也確實(shí)報(bào)錯(cuò)了,但并不會(huì)終止這個(gè)腳本,即這個(gè)腳本再服務(wù)器內(nèi)執(zhí)行的退出碼為0
參考《ES6標(biāo)準(zhǔn)入門(mén)》(第3版) P280~P282
總結(jié)
以上是生活随笔為你收集整理的es6 --- Promise.catch的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android avrcp处理流程,an
- 下一篇: vscode更换主题的插件_VScode