1. 1.promise的几种状态及状态的切换
  2. 2.thencatch的返回值
  1. 1.promise的三种状态
  2. - pending fulfilled rejected
  3. 2.触发状态的
  4. pending -->fulfilled
  5. pending -->rejected
  6. 变化的状态是不可逆的,一旦变化就不会返回。
  7. 3.thencatch对状态的影响 (thencatch的链式调用)

1 状态的变化

1-1 pending 冻结

  1. var p = new Promise((resolve,reject)=>{
  2. })
  3. console.log(p)

1-2 pending - resolve

  1. var p = new Promise((resolve,reject)=>{
  2. setTimeout(()=>{
  3. resolve()
  4. })
  5. })
  6. console.log(p)
  7. setTimeout(()=>{
  8. console.log(p)
  9. })

2 触发状态状

  1. 1.pending状态不会触发resolvereject
  2. 2.resolve会触发之后的then回调函数
  3. 3.reject会触发catch回调函数
  1. var p = Promise.resolve(100);
  2. var r = Promise.reject(200);

3 then和catch改变状态 返回值

  1. 1.then正常返回resolve,有报错则返回reject
  2. 2.catch正常返回resolve,有报错则返回reject

3-1 pending —>resolve

  1. var p = Promise.resolve(100).then(res=>{
  2. console.log(res)
  3. })
  4. console.log(p) //返回
  1. //没有报错可以继续then
  2. p.then(()=>{
  3. console.log(200)
  4. })

3-2 then函数中有报错,返回reject

  1. var p = Promise.resolve(100).then(res=>{
  2. console.log(res)
  3. throw "不是函数"
  4. })
  5. console.log(p)

3-3 有报错继续then函数

  1. var p = Promise.resolve(100).then(res=>{
  2. throw "error"
  3. })
  4. p.then(()=>{
  5. console.log("good")
  6. }).catch(res=>{
  7. console.log(res)
  8. })

3-4 catch之报错

  1. var p = Promise.reject("err").catch(err=>{
  2. console.log(err)
  3. throw "fun"
  4. })
  5. console.log(p) //返回的promise的状态为 reject