https://eyesofkids.gitbooks.io/javascript-start-es6-promise/content/contents/ch5_flow_n_error.html
// 用throw語句取代reject
const p1 = new Promise((resolve, reject) => {
throw new Error('rejected!') // 用throw語句
//相當於用以下的語句
//reject(new Error('rejected!'))
})
p1.then(val => {
console.log(val)
return val + 2
})
.then(val => console.log(val))
.catch(err => console.log('error:', err.message))
.then(val => console.log('done'))
//最後結果:
//error: rejected!
//done
沒有留言:
張貼留言