首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >等待2种方法完成角度6

等待2种方法完成角度6
EN

Stack Overflow用户
提问于 2018-09-03 10:41:51
回答 2查看 2.7K关注 0票数 2

我有两个方法,使用服务逻辑中提供的REST请求返回两个不同的数组:

代码语言:javascript
复制
 cartItemNodes: TreeNode[] = [];
 cartGroupNodes: TreeNode[] = [];

 getCartItems(){
  //subscribe to service observable filling the array 
  return this.cartItemNodes;
}

 getCartGroups(){
  //subscribe to service observable filling the array 
  return this.cartGroupNodes;
}

如何构建第三种方法?

getCartFinalNodes()

,哪个等待到第一个,两个完成,然后将它们的结果组合成一个数组?

代码语言:javascript
复制
getCartFinalNodes(){
//wait for first 2 methods
return this.cartItemNodes.concat(this.cartGroupNodes);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-03 10:54:11

首先从两种方法返回承诺,然后使用Promise.all,如下所示

代码语言:javascript
复制
  Promise.all([
   firstMethod(key1),
   seondMethod(key2),
  ]).then(value => thirdMethod());
票数 3
EN

Stack Overflow用户

发布于 2018-09-03 11:37:55

使用允诺API:

代码语言:javascript
复制
getCartItems() {
    return new Promise((resolve, reject) => {
        resolve(this.cartItemNodes);
    });
}

getCartGroups() {
    return new Promise((resolve, reject) => {
        resolve(this.cartGroupNodes);
    });
}

Promise.all([
    this.getCartItems(),
    this.getCartGroups(),
    ]).then(value => this.getCartFinalNodes());
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52148096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档