vue中Promise的用法
promise是什么?1、主要用于异步计算2、可以将异步操作队列化,按照期望的顺序执行,返回符合预期的结果3、可以在对象之间传递和操作promise,帮助我们处理队列为什么会有promise?为了解决处理回掉地狱1.promise简单用法function2(){// 你的逻辑代码return Promise.resolve(/* 这里是需要返回的数据*/)}function3(){// 你的逻辑代
   ·  
 promise是什么?
  1、主要用于异步计算
  2、可以将异步操作队列化,按照期望的顺序执行,返回符合预期的结果
  3、可以在对象之间传递和操作promise,帮助我们处理队列
为什么会有promise?
为了解决处理回掉地狱
1.promise简单用法
function2(){
    // 你的逻辑代码 
    return Promise.resolve(/* 这里是需要返回的数据*/)
}
function3(){
    // 你的逻辑代码 
    return Promise.resolve(/* 这里是需要返回的数据*/)
}
// 调用
function1(){
    this.function2().then(val => { 
        this.function3();
    });
}2.promise高阶用法
        new Promise((resolve, reject) => {
          let a = 1
          resolve(a) // 外部可用then接收参数
          let b = 'error'
          reject(b) // 外部可用catch接收参数
        })3.promiss.all (一般用于列表编辑和查看详情)
      init() {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.roleIdListDefault = []
          Promise.all([
            this.getDicGender(),
          ]).then(() => {
            if (this.dataForm.id) {
              this.getInfo()
            }
          })
        })
      },
      // 获取性别字典
      getDicGender() {
        const url = `/sys/dict/list`
        return this.$http.get(url).then(({
          data: res
        }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.genderList = res.data
        }).catch(() => {})
      },等获取字典的接口执行完,拿到返回值后 在执行 getInfo()获取详情的操作
更多推荐
 
 



所有评论(0)