toPromise

signature: toPromise() : Promise

Convert observable to promise.

Examples

Example 1: Basic Promise

( jsBin | jsFiddle )

import { of } from 'rxjs/observable/of';
import { toPromise, delay } from 'rxjs/operators';

//return basic observable
const sample = val => of(val).pipe(delay(5000));
//convert basic observable to promise
const example = sample('First Example')
  .pipe(toPromise())
  //output: 'First Example'
  .then(result => {
    console.log('From Promise:', result);
  });
Example 2: Using Promise.all

( jsBin | jsFiddle )

import { of } from 'rxjs/observable/of';
import { delay } from 'rxjs/operators';

//return basic observable
const sample = val => of(val).pipe(delay(5000));
/*
  convert each to promise and use Promise.all
  to wait for all to resolve
  (you should probably use forkJoin and no 'toPromise' instead!)
*/
const example = () => {
  return Promise.all([
    sample('Promise 1').pipe(toPromise()),
    sample('Promise 2').pipe(toPromise())
  ]);
};
//output: ["Promise 1", "Promise 2"]
example().then(val => {
  console.log('Promise.all Result:', val);
});

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/toPromise.ts

results matching ""

    No results matching ""