promise 核心技术3 使用

什么是promise?(加深理解)

抽象表达:(比较高的高度 看这门技术)

Promise是js中进行异步操作的新的解决方案(旧形式:纯回调的形式)

具体表达:

  • 从语法上,Promise是一个构造函数
  • 从功能上,promise对象用来封装一个异步操作并可以获取结果

Promise的状态改变

1.pending变为resolved

2.pending变为rejected

一个promise对象只能改变一次。无论成功或者失败,都有一个结果

成功的结果value 失败的结果reason

Promise的基本流程

待补全

promise基本使用

  const proxy = new Promise((resolve,reject) => {
    setTimeout( () => {
      const time = Date.now()
      if (time%2 === 1) {
        resolve('成功的数据1,time=' + time) //成功调用resolve(value)
      } else {
        reject('失败的数据1,time=' + time)
      }}, 0)
  })
  proxy.then(
    value => {
      console.log("成功的回调2", value) //接受成功的数据 onResolve
    },
    reason => {
      console.log("失败的回调2", reason) //接受失败的数据 onReject
    }
  )

111

猜你喜欢

转载自www.cnblogs.com/-constructor/p/12216134.html
今日推荐