定义

  1. Promise是一种异步操作的解决方案,将写法复杂的传统的回调函数和监听事件的异步操作,用同步代码的形式表达出来。
  2. 避免了多级异步操作的回调函数嵌套。
  3. 目的:为了解决异步任务而诞生,可以将一个异步的流程变为同步的

promise的三种状态

  1. 等待状态
  2. resolve 成功的状态 使用then调用
  3. reject 失败的状态 使用catch调用
  1. <script>
  2. /* promise监听 */
  3. var p = new Promise((resolve, reject) => {
  4. resolve("success");
  5. reject("error")
  6. })
  7. /*
  8. resolve then
  9. reject catch
  10. */
  11. p.then(res=>{
  12. console.log(res)
  13. })
  14. </script>
  1. <script>
  2. /* promise监听 */
  3. var p = new Promise((resolve, reject) => {
  4. // resolve("success");
  5. reject("error")
  6. })
  7. /*
  8. resolve then
  9. reject catch
  10. */
  11. p.catch(res=>{
  12. console.log(res)
  13. })
  14. </script>