首页 > 其他 > 详细

Angular:Promise.all()的具体应用

时间:2018-05-25 16:36:47      阅读:198      评论:0      收藏:0      [点我收藏+]

现有项目开发中,从后台获取数据用异步promise,可是异步的数据要顺序获取真的是好费劲啊,还好解锁了promise.all(),下面来看代码吧!

遍历this.groupList通过groupID获取组内成员,将获取的成员push到finalAccount数组,this.groupList遍历完成以后,需要将finalAccount发送到父组件内,可是异步的话怎么发都有问题惹,改成promise.all()问题就解决了。

 const array = [];
    this.groupList.forEach(
      x => {
        if (x.checked) {
          const promise1 = this.customService.getCustomGroupUsers(x.id).then(res => {
            this.cs.log(promise1);
            for (const user of res.users) {
              this.finalAccount.push({
                groupId: x.id,
                groupName: x.name,
                openId: user.openId,
                userName: user.userName,
              })
            }
            return Promise.resolve(res.users);
          });
          array.push(promise1);
        }
      }
    );

    Promise.all(array)
    .then(() => {
      console.log(emit);
      this.cs.log(emit);
      this.onFinalAccount.emit(this.finalAccount);
      this.onCloseLink.emit(false);
    });

Angular:Promise.all()的具体应用

原文:https://www.cnblogs.com/xiaojing140421/p/9089187.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!