状态

pending 等待
resolved 成功
rejected 失败

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

then和catch的返回值

then和catch返回值
1.都会返回promise
2.正常情况下状态为resolve,遇到错误状态为rejected

  1. var p = Promise.resolve(100).then(res=>{
  2. console.log(res)
  3. throw "err"
  4. })
  5. p.then(()=>{
  6. console.log(200)
  7. }).catch(err=>{
  8. console.log(err)
  9. }).then(()=>{
  10. console.log(300)
  11. })
  12. </script>

异常处理

  1. (async () => {
  2. var p = Promise.reject("err");
  3. try {
  4. let res = await p;
  5. console.log(res)
  6. }catch(err){
  7. console.log(err)
  8. }
  9. })()
  1. Promise.reject("err").catch((err)=>{
  2. console.log(err)
  3. })