Q) How do I convert the following observable to a promise so I can call it with .then(...)
?
My method I want to convert to a promise:>
A lot of comments are claiming toPromise
deprecated but as you can see here it's not.
So please juste use toPromise
(RxJs 6) as said:
//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);
});
async/await example:
//return basic observable
const sample = val => Rx.Observable.of(val).delay(5000);
//convert basic observable to promise
const example = await sample('First Example').toPromise()
// output: 'First Example'
console.log('From Promise:', result);
Read more here.
And please remove this wrong claim saying toPromise
is deprecated.
Note: Otherwise you can use .pipe(take(1)).toPromise
but as said you shouldn't have any problem using above example.