44
55## Convert observable to promise.
66
7+ ---
8+
9+ :warning : ` toPromise ` has been deprecated! (RxJS 5.5+)
10+
11+ ---
12+
713<div class =" ua-ad " ><div class =" ua-ad " ><a href =" https://ultimateangular.com/?ref=76683_kee7y7vk " ><img src =" https://ultimateangular.com/assets/img/banners/ua-leader.svg " ></a ></div ></div >
814
915### Examples
1420[ jsFiddle] ( https://jsfiddle.net/btroncone/thykc9up/ ) )
1521
1622``` js
17- // RxJS v6+
18- import { of } from ' rxjs/observable/of' ;
19- import { toPromise , delay } from ' rxjs/operators' ;
20-
2123// return basic observable
22- const sample = val => of (val).pipe ( delay (5000 ) );
24+ const sample = val => Rx . Observable . of (val).delay (5000 );
2325// convert basic observable to promise
2426const example = sample (' First Example' )
25- .pipe ( toPromise () )
27+ .toPromise ()
2628 // output: 'First Example'
2729 .then (result => {
2830 console .log (' From Promise:' , result);
@@ -35,20 +37,16 @@ const example = sample('First Example')
3537[ jsFiddle] ( https://jsfiddle.net/btroncone/xzu6u7hs/ ) )
3638
3739``` js
38- // RxJS v6+
39- import { of , delay } from ' rxjs' ;
40-
4140// return basic observable
42- const sample = val => of (val).pipe ( delay (5000 ) );
41+ const sample = val => Rx . Observable . of (val).delay (5000 );
4342/*
4443 convert each to promise and use Promise.all
4544 to wait for all to resolve
46- (you should probably use forkJoin and no 'toPromise' instead!)
4745*/
4846const example = () => {
4947 return Promise .all ([
50- sample (' Promise 1' ).pipe ( toPromise () ),
51- sample (' Promise 2' ).pipe ( toPromise () )
48+ sample (' Promise 1' ).toPromise (),
49+ sample (' Promise 2' ).toPromise ()
5250 ]);
5351};
5452// output: ["Promise 1", "Promise 2"]
@@ -59,7 +57,7 @@ example().then(val => {
5957
6058### Additional Resources
6159
62- * [ toPromise] ( https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/topromise.md )
60+ - [ toPromise] ( https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/topromise.md )
6361 :newspaper : - Official Docs
6462
6563---
0 commit comments