RxJS: splitting an array into the result of Observable.fromPromise

I am using RxJS here and I do not seem to overcome this seemingly simple problem.

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 code above splashes out each element of the array in the 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); }); } 

Here, although I only get the full array (ie [ 1, 2, 3, 54, 3, 22, 323, 23, 11, 2 ] ). Doesn't RxJS break the result? I hope he at least had some kind of logic for this.

thanks

+7
arrays reactive-programming rxjs
source share
1 answer

No, this will not break them implicitly. If you want to separate them, use flatMap , which will flatMap 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'); }); 
+15
source share

All Articles