zoukankan      html  css  js  c++  java
  • ES6中的Promise

    Promise

    Promise 翻译过来就是承诺的意思,这个承诺会在未来有一个确切的答复,并且该承诺有三种状态,这个承诺一旦从等待状态变成为其他状态就永远不能更改状态了。

    • 等待中(pending)
    • 完成了(resolved)更准确的应该说是(fulfilled)
    • 拒绝了(rejected)

    当我们在构造 Promise 的时候,构造函数内部的代码是立即执行的。

    new Promise((resolve, reject) => {
      console.log('new Promise')
      resolve('success')
    })
    console.log('finifsh')
    
    // 先打印new Promise, 再打印 finifsh
    

    Promise 实现了链式调用,也就是说每次调用 then 之后返回的都是一个 Promise,并且是一个全新的 Promise,原因也是因为状态不可变。如果你在 then 中 使用了 return,那么 return 的值会被 Promise.resolve() 包装。

    Promise.resolve(1)
      .then(res => {
        console.log(res) // => 1
        return 2 // 包装成 Promise.resolve(2)
      })
      .then(res => {
        console.log(res) // => 2
      })
    

    当然了,Promise 也很好地解决了回调地狱的问题

    ajax(url)
      .then(res => {
          console.log(res)
          return ajax(url1)
      }).then(res => {
          console.log(res)
          return ajax(url2)
      }).then(res => console.log(res))
    

    其实它也是存在一些缺点的,比如无法取消 Promise,错误需要通过回调函数捕获。

    今天你学习了吗!!!
  • 相关阅读:
    面向对象与组合
    异常处理和三级菜单练习
    装饰器和生成器
    序列化模块
    leetcode_498. 对角线遍历
    leetcode_566. 重塑矩阵
    leetcode_59. 螺旋矩阵 II
    leetcode_54. 螺旋矩阵
    leetcode_396. 旋转函数
    leetcode_200. 岛屿数量
  • 原文地址:https://www.cnblogs.com/nayek/p/11728855.html
Copyright © 2011-2022 走看看