Promise链式调用小笔记

1.需要多次循环请求,并且最后一次请求结束后返回请求信息

 Promise.all(
          this.imgList.map((item) => {
            return new Promise((resolve, reject) => {
              // 替换为你的请求
              this.$http
                .post(`${window.ROOT_URL}api/v2/user/upload/doAction`, {
                  image: item.content,
                  directory: "comment",
                  type: "image",
                })
                .then((res) => {
                  if (res.data.code == 0) {
                    console.log(res.data,'666');
                    var imageObj = {
                      file_image: res.data.data.fileUrl,
                      file_thumb: res.data.data.thumb_url,
                    };
                    this.commentUrl.push(imageObj);
                  } else {
                    this.commentUrl = [];
                  }
                  //所以请求结束后拿到的数据
                  return resolve({
                    code: res.data.code,
                    message: res.data.message,
                  });
                });
            });
          })
        ).then((res) => {
          const result = res.filter((v) => {
            return v.code === 1;
          });
          if (result.length > 0) {
            Toast(` 图片大小不能超过${result[0].message[6]}M,请重新上传。`);
          }
          else {
            const newFormData = {
              comment_image: this.commentUrl,
              content: this.textarea,
              comment_id: 0,
              comment_rank: Number(this.rank),
              rec_id: Number(this.$route.params.id),
            };
            this.$store
              .dispatch("setAddgoodscomment", newFormData)
              .then((res) => {
                if (res.code == 0) {
                  this.$router.go(-1);
                  // this.$router.push({
                  //     name: 'comment'
                  // });
                  Toast.clear();
                  Toast({
                    message: "评论成功",
                    duration: 1000,
                  });
                } else {
                  Toast.clear();
                  Toast(this.$t("lang.comment_fail"));
                }
              })
              .catch((err) => {
                Toast.clear();
                Toast(this.$t("lang.comment_fail"));
              });
          }
        });
      }

2.连续.then调用

 //1.返回一个Promise
 getAesEncode() {
          return new Promise((resolve,reject)=>{
            this.$http
                .get(`${window.ROOT_URL}api/v2/common/getAesEncode`)
                .then((res) => {
                    resolve(res.data.data)
                    this.aesEncodeKey = res.data.data;
                });
          })
     },

 //2.直接.then调用
 this.getAesEncode().then(res=>{
    //调用成功的函数
    this.handleLogin();
 })

3.通过await异步接收需要的信息

 //1.返回一个Promise
 async getAesEncode() {
          return new Promise((resolve,reject)=>{
            this.$http
                .get(`${window.ROOT_URL}api/v2/common/getAesEncode`)
                .then((res) => {
                    resolve(res.data.data)
                });
          })
     },

 //2.await接收调用
const promiseData = await this.getAesEncode()
console.log(promiseData,'promise成功之后返回的数据')

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值