I have an observable (rxjs) that sends data using subscriber.next(value)
.
How to get these data if the observable is converted to promise via the method toPromise()
? Is that even possible? Or is it only possible to get the data passed through the complete method of the observable.
Maybe there are options / methods in pipe, before converting to promise to get all data from next()
as an array in the data field of the then()
method of the promise.
getObservable().toPromise().then(data => {
// data should contain all data sent from observable.next()
console.log('Complete: ', data);
}, error => {
console.log('Error: ', error);
});
Current behavior (expected but not wanted in this case):
data
contains the data of observable.complete().
Wanted behavior:
data
contains an array of all observable.next().
This should enabled by an option or pipe.
Example with Observable.range():
let observer = Rx.Observable.range(1,5).toPromise().then(data => {
console.log('Completed: ', data);
}, error => {
console.log('Error: ', error);
});
Output:
Completed: 5
Wanted:
Completed: [1, 2, 3, 4, 5]
I'm not sure if this is even possible.
Reduce before you make it a promise
const source = Rx.Observable.of(1, 2, 3, 4);
const example = source.reduce((acc,val) => acc.concat(val), []);
example.toPromise().then((data) => {
alert(data);
});