我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

  • 基础

    • js

    • css

    • ES6

      • 说说对模块化的理解
      • 说说ES6中对象新增了哪些扩展
      • 说说ES6中函数新增了哪些扩展
      • ES6中数组的常用方法
      • 说说ES6中新增的Set、Map的区别
      • 说说ES6中Decorator的理解
      • 说说ES6中Generator的应用
      • 说说ES6中Promise的常用方法及原理
        • 是什么
        • 怎么用
          • 状态
          • 流程
          • 实例方法
          • 构造函数方法
        • 原理
        • FAQ
      • 说说ES6中Proxy的理解
      • 说说var、let、const之间的区别
  • 框架

  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 基础
  • ES6
fangdown
2021-08-18
目录

说说ES6中Promise的常用方法及原理

# 是什么

Promise:是异步编程的一种解决方案,一个函数

  • 优点
    • 解决回调地域
    • 链式调用

# 怎么用

# 状态

  • pending 进行中
  • fulfilled 已成功
  • rejected 已失败

    状态一经改变就不会再变化,任何时候都可以得到结果

# 流程

# 实例方法

  • then
  • catch
  • finally
const promise = new Promise(function(reslove, reject)=>{})
promise.then(res =>{})
promise.then(res =>{}).catch(error => console.log(error))
promise.then(res =>{}).catch(error => console.log(error)).finally(()=>console.log('end'))

# 构造函数方法

  • all
  • race
  • resolve
  • reject
const p1 = new Promise(function(reslove, reject)=>{})
const p2 = new Promise(function(reslove, reject)=>{})
const p3 = new Promise(function(reslove, reject)=>{})
const res1 = Promise.all([p1, p2, p3]) // 全部要得到结果
const res2 = Promise.race([p1, p2, p3]) // 只要一个得到结果
Promise.resolve('result')
// ===
new Promsie(resolve => resolve('result'))

Promise.reject('reason')
// ===
new Promise((resolve, reject) => reject('reason'))

# 原理

  • 链式调用原理? 每次都返回一个新的promise

  • promise实现

    • 构造函数属性
      • status
      • value
      • onResolveCallbacks
      • onRejectCallbacks
    • 构造函数方法
      • resolve
      • reject

# FAQ

#promise
上次更新: 2021/12/19, 18:05:42
说说ES6中Generator的应用
说说ES6中Proxy的理解

← 说说ES6中Generator的应用 说说ES6中Proxy的理解→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式