RxJS Observables 的 Promise.all 行为? [英] Promise.all behavior with RxJS Observables?

查看:60
本文介绍了RxJS Observables 的 Promise.all 行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Angular 1.x 中,我有时需要发出多个 http 请求并对所有响应进行处理.我会将所有的 promise 扔到一个数组中并调用 Promise.all(promises).then(function (results) {...}).

In Angular 1.x I would sometimes need to make multiple http requests and do something with all the responses. I would throw all the promises in an array and call Promise.all(promises).then(function (results) {...}).

Angular 2 最佳实践似乎指向使用 RxJS 的 Observable 作为 http 请求中承诺的替代.如果我从 http 请求创建了两个或多个不同的 Observable,是否有与 Promise.all() 等效的东西?

Angular 2 best practices seem to point towards the use of RxJS's Observable as a replacement to promises in http requests. If I have two or more different Observables created from http requests, is there an equivalent to Promise.all()?

推荐答案

模拟 Promise.all 的更直接的替代方法是使用 forkJoin 操作符(它开始所有observables 并行并加入它们的最后一个元素):

The more straightforward alternative for emulating Promise.all is to use the forkJoin operator (it starts all observables in parallel and join their last elements):

有点超出范围,但如果它有帮助,关于链接承诺的主题,您可以使用一个简单的 flatMap :Cf.RxJS Promise 组合(传递数据)

A bit out of scope, but in case it helps, on the subject of chaining promises, you can use a simple flatMap : Cf. RxJS Promise Composition (passing data)

这篇关于RxJS Observables 的 Promise.all 行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆