RxJS:从 Observable.fromPromise 拆分数组结果 [英] RxJS: Splitting an array result from Observable.fromPromise
本文介绍了RxJS:从 Observable.fromPromise 拆分数组结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里使用 RxJS,但我似乎无法克服这个看似简单的问题.
I'm using RxJS here and I can't seem to get over this seemingly simple issue.
rx.Observable
.from([1,2,3,54,3,22,323,23,11,2])
.distinct()
.subscribe(function next (x) {
console.log('Next');
console.log(x);
}, function error (x) {
console.log('Error');
console.log(x);
}, function completed () {
console.log('Completed');
});
上面的代码按预期顺序吐出每个数组项.
The above code spits out each array item in order as expected.
rx.Observable
.fromPromise(getNumbers([1,2,3,54,3,22,323,23,11,2]))
.distinct()
.subscribe(function next (x) {
console.log('Next');
console.log(x);
}, function error (x) {
console.log('Error');
console.log(x);
}, function completed () {
console.log('Completed');
});
function getNumbers (nums) {
return new Promise(function (resolve, reject) {
resolve(nums);
});
}
这里虽然我只得到了完整的数组(即 [ 1, 2, 3, 54, 3, 22, 323, 23, 11, 2 ]
).RxJS 不应该把结果分开吗?我希望它至少有一些逻辑.
Here though I only get the full array (i.e. [ 1, 2, 3, 54, 3, 22, 323, 23, 11, 2 ]
). Shouldn't RxJS be breaking the result apart? I'd hope it at least had some logic for this.
谢谢
推荐答案
不,它不会隐式地将它们分开.如果要拆分它们,请使用 flatMap
将数组展平:
No it will not break them apart implicitly. If you want to split them use flatMap
which will flatten the array:
rx.Observable
.fromPromise(getNumbers([1,2,3,54,3,22,323,23,11,2]))
.flatMap(function(x) { return x; })
.distinct()
.subscribe(function next (x) {
console.log('Next');
console.log(x);
}, function error (x) {
console.log('Error');
console.log(x);
}, function completed () {
console.log('Completed');
});
这篇关于RxJS:从 Observable.fromPromise 拆分数组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文