Javascript 将每个数组项的承诺添加到'Promise.all()中`

Javascript 将每个数组项的承诺添加到'Promise.all()中`,javascript,angular,typescript,promise,rxjs,Javascript,Angular,Typescript,Promise,Rxjs,我有下面的代码,其中我迭代了一个带有名字的列表,并在本例中使用每个名字来获取相应的房子。这很好,但我希望将所有承诺都包含到Promise.all()-如何动态地将每个名称的承诺添加到Promise.all()?这里的好习惯是什么 list = [ 'Name1', 'Name2', 'Name3' ]; this.houses = new BehaviorSubject<any>([]); const promises = this.greatHouses.map(n

我有下面的代码,其中我迭代了一个带有名字的列表,并在本例中使用每个名字来获取相应的房子。这很好,但我希望将所有承诺都包含到
Promise.all()
-如何动态地将每个名称的承诺添加到
Promise.all()
?这里的好习惯是什么

list = [
  'Name1',
  'Name2',
  'Name3'
];

this.houses = new BehaviorSubject<any>([]);

const promises = this.greatHouses.map(name => {
  let house;
  return this.fetchHouse(name).then((result: any[]) => {
    house = result[0];
    return result[0];
  }).then((result: any) => {
    return this.fetchLord(result.currentLord);
  }).then((result: any) => {
    const currentLord = Characters.default.find(char => char.characterName === result.name);
    return [{...house, currentLord}];
  });
});

Promise.all(promises).then(values => {
  console.log(values);
});

fetchHouse(name: string) {
  return this.http.get(`${this.housesUrl}`, {
    params: {
      name
    }
  }).toPromise();
}

fetchLord(url: string) {
  return this.http.get(url).toPromise();
}
列表=[
“名称1”,
“名称2”,
“名称3”
];
this.houses=新行为主体([]);
const promises=this.greatHouses.map(name=>{
出租房屋;
返回此.fetchHouse(名称)。然后((结果:any[])=>{
豪斯=结果[0];
返回结果[0];
})。然后((结果:任意)=>{
返回此.fetchLord(result.currentLord);
})。然后((结果:任意)=>{
const currentLord=Characters.default.find(char=>char.characterName===result.name);
返回[{…房子,当前主];
});
});
承诺。所有(承诺)。然后(值=>{
console.log(值);
});
fetchHouse(名称:string){
返回this.http.get(`${this.housesUrl}`{
参数:{
名称
}
}).toPromise();
}
fetchLord(url:string){
返回此.http.get(url.toPromise();
}

根本不要使用承诺。如果您正在使用Angular及其HttpClient,请学习使用Observable

greathouse=[
“名称1”,
“名称2”,
“名称3”
];
//构造并排列获取数据的可观察对象
const houses$=this.greatHouses.map(name=>
此.fetchHouse(名称).pipe(
//将结果映射到房屋
map((结果:any[])=>result[0]),
//将house映射到一个可观察对象,该对象获取主并返回所需数据
switchMap((house:any)=>this.fetchLord(house.currentLord).pipe(
//把领主映射到领主的家里
地图((领主:任何)=>{
const currentLord=Characters.default.find(char=>char.characterName===lord.name);
返回{…家,主};
})
))
)
);
//用forkJoin将一组完整的可观察对象组合到一个可观察对象
forkJoin(houses$).subscribe(houses=>console.log('houses',houses))
//返回可观测值,并在错误发生的地方直接使用“catchError”处理错误。
fetchHouse(名称:string){
返回this.http.get(`${this.housesUrl}`{
参数:{
名称
}
}).烟斗(
catchError(错误=>{
console.error('fetchHouse failed',error);
返回(空);
})
);
}
fetchLord(url:string){
返回此.http.get(url).pipe(
catchError(错误=>{
console.error('fetchLord failed',error);
返回(空);
})
);
}

只需收集您在数组中创建的承诺,然后调用
Promise。所有的
都是这样吗?最好的做法是使用
map
而不是在
list
数组上循环,但两者都可以。我个人会使用
this.list.map
返回这个.fetchHouse。。。。etc
那么返回的数组可以使用Promise.allBtw,关于
出租
,还有更好的。不要忘了在最后添加一个
.catch(console.error)
。更新后的问题中的代码应该有效。你的单个承诺也有效,你只是从来没有注意到它,因为你根本没有处理错误。这是一个可观察到的粉丝的解决方案。承诺更加直截了当。:-)@ROAME-1888,不管你认为什么都是直截了当的,很大程度上取决于你所知道的最好的东西。因此,一个人可能认为承诺的方式是直截了当的,而另一个人则认为显而易见的方式是直截了当的。老年退休金问题的目标是有角度的。RxJS是Angular的核心依赖项,在Angular中大量使用。任何使用Angular的人都应该熟悉并熟悉RxJS。在这种情况下,使用
toPromise
在每个可观察到的人遇到的情况下都是不好的做法。观测值通常更灵活,因为它们可以以多种方式与其他观测值和事件相结合。