vue中Promise的用法

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()获取详情的操作

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值