我有两个方法,使用服务逻辑中提供的REST请求返回两个不同的数组:
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()
,哪个等待到第一个,两个完成,然后将它们的结果组合成一个数组?
getCartFinalNodes(){
//wait for first 2 methods
return this.cartItemNodes.concat(this.cartGroupNodes);
}发布于 2018-09-03 10:54:11
首先从两种方法返回承诺,然后使用Promise.all,如下所示
Promise.all([
firstMethod(key1),
seondMethod(key2),
]).then(value => thirdMethod());发布于 2018-09-03 11:37:55
使用允诺API:
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());https://stackoverflow.com/questions/52148096
复制相似问题