(基础)Promise类方法reject()使用

reject()可直接返回Promise失败对象

const promise = Promise.reject("rejected message")
// 相当于
// const promise2 = new Promsie((resolve, reject) => {
//   reject("rejected message")
// })

注意: 无论传入什么值都是一样的
都是捕获错误信息

传入Promise

const promise = Promise.reject(new Promise(() => {}))
promise.then(res => {
  console.log("res:", res)
}).catch(err => {
  console.log("err:", err)     //err: Promise {  } 
})

 传入Promise

const promise = Promise.reject(new Promise((resolve, reject) => {
  resolve('1111')
}))
promise.then(res => {
  console.log("res:", res)
}).catch(err => {
  console.log("err:", err)     //err: Promise { '1111' }
})

传入thenable对象

const promise = Promise.reject({
    then: function(resolve, reject) {
      resolve(222222)     //不会调用
    }
})
promise.then(res => {
  console.log("res:", res)
}).catch(err => {
  console.log("err:", err)     //err: { then: [Function: then] }  
})

你可能感兴趣的:(javaScript,vue.js,javascript)