【发布时间】:2021-12-10 14:12:52
【问题描述】:
我正在使用 NestJS HttpModule 为一个端点发出 GET 请求。代码有点像这样:
@Injectable
export class AnimalService {
constructor(private httpService: HttpService){}
getAnimalData(variant: string): Observable<AxiosResponse<Animal>> {
return this.httpService
.get(`http://animal.test/${variant}`)
.pipe(map((response) => response.data));
}
}
现在我想创建一个同时调用多个端点的方法。
getAllAnimalsData() {
// const variants = ['birds', 'cats', 'dogs'];
// call
// http://animal.test/birds
// http://animal.test/cats
// http://animal.test/dogs
// simultaneously
// and process the response data
}
如何使用 NestJS HttpModule 实现这一点?
我如何处理每个结果?
如果出现部分错误(例如 3 个请求中有 1 个有错误),我该如何处理?
【问题讨论】:
-
打多个端点似乎不是一个好主意,为什么不创建一个返回数组本身的端点?
-
问题是那些端点来自第三方。我不能直接更改它们或建议他们添加更多端点
标签: node.js axios rxjs observable nestjs