Javascript RxJS Observables 的 Promise.all 行为?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35608025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Promise.all behavior with RxJS Observables?
提问by Corey Ogburn
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 1.x 中,我有时需要发出多个http
请求并对所有响应进行处理。我会将所有的 promise 放入一个数组中并调用Promise.all(promises).then(function (results) {...})
.
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()
?
Angular 2 最佳实践似乎指向使用 RxJS 来Observable
替代http
请求中的承诺。如果我从 http 请求创建了两个或多个不同的 Observable,是否有等价于Promise.all()
?
采纳答案by user3743222
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):
模拟更直接的替代方法Promise.all
是使用forkJoin
运算符(它并行启动所有可观察对象并连接它们的最后一个元素):
- documentation
- related link: Cf. RxJS: concat three promises, distinguish results
- 文件
- 相关链接:参见 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)
有点超出范围,但如果有帮助,在链接承诺的主题上,您可以使用一个简单的flatMap
: Cf. RxJS Promise 组合(传递数据)
回答by kakigoori
forkJoin works fine too, but I'd prefer combineLatestsince you don't need to worry about it taking the last value of observables. This way, you can just get updated whenever any of them emit a new value too (e.g. you fetch on an interval or something).
forkJoin 也可以正常工作,但我更喜欢combineLatest ,因为您无需担心它会获取 observables 的最后一个值。这样,只要它们中的任何一个也发出新值,您就可以得到更新(例如,您在某个时间间隔内获取)。
回答by arcseldon
Update May 2019 using RxJs v6
使用 RxJs v6 更新 2019 年 5 月
Found the other answers useful, and wished to offer an example for the answer offered by Arnaud about zip
usage.
发现其他答案很有用,并希望为 Arnaud 提供的有关zip
用法的答案提供一个示例。
Here is a snippet showing the equivalence between Promise.all
and the rxjs zip
(note also, in rxjs6 how zip now gets imported using "rxjs" & not as an operator).
这是一个片段,显示了Promise.all
和 rxjs之间的等价性zip
(还要注意,在 rxjs6 中,现在如何使用“rxjs”而不是作为运算符导入 zip)。
import { zip } from "rxjs";
const the_weather = new Promise(resolve => {
setTimeout(() => {
resolve({ temp: 29, conditions: "Sunny with Clouds" });
}, 2000);
});
const the_tweets = new Promise(resolve => {
setTimeout(() => {
resolve(["I like cake", "BBQ is good too!"]);
}, 500);
});
// Using RxJs
let source$ = zip(the_weather, the_tweets);
source$.subscribe(([weatherInfo, tweetInfo]) =>
console.log(weatherInfo, tweetInfo)
);
// Using ES6 Promises
Promise.all([the_weather, the_tweets]).then(responses => {
const [weatherInfo, tweetInfo] = responses;
console.log(weatherInfo, tweetInfo);
});
The output from both are the same. Running the above gives:
两者的输出是一样的。运行上面给出:
{ temp: 29, conditions: 'Sunny with Clouds' } [ 'I like cake', 'BBQ is good too!' ]
{ temp: 29, conditions: 'Sunny with Clouds' } [ 'I like cake', 'BBQ is good too!' ]
回答by Arnaud P
On reactivex.ioforkJoinactually points to Zip, which did the job for me:
在reactivex.io forkJoin实际指向的邮编,这做的工作对我来说:
let subscription = Observable.zip(obs1, obs2, ...).subscribe(...);