toPromise
signature: toPromise() : Promise
Convert observable to promise.
toPromise
is not a pipable operator, as it does not return an observable.
Examples
Example 1: Basic Promise
//return basic observable
const sample = val => Rx.Observable.of(val).delay(5000);
//convert basic observable to promise
const example = sample('First Example')
.toPromise()
//output: 'First Example'
.then(result => {
console.log('From Promise:', result);
});
Example 2: Using Promise.all
//return basic observable
const sample = val => Rx.Observable.of(val).delay(5000);
/*
convert each to promise and use Promise.all
to wait for all to resolve
*/
const example = () => {
return Promise.all([
sample('Promise 1').toPromise(),
sample('Promise 2').toPromise()
]);
};
//output: ["Promise 1", "Promise 2"]
example().then(val => {
console.log('Promise.all Result:', val);
});
Additional Resources
- toPromise - Official Docs
Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/toPromise.ts