toPromise

signature: toPromise() : Promise

Convert observable to promise.


โš  toPromise is not a pipable operator, as it does not return an observable.


[![Ultimate RxJS](https://drive.google.com/uc?export=view&id=1qq2-q-eVe-F_-d0eSvTyqaGRjpfLDdJz "Ultimate RxJS")](https://ultimatecourses.com/courses/rxjs?ref=4)

Examples

Example 1: Basic Promise

( jsBin | jsFiddle )

//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

( jsBin | jsFiddle )

Additional Resources


๐Ÿ“ Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/toPromise.ts

Last updated