How to convert Promise to Observable in Angular?

How to convert Promise to Observable in Angular?

What is Observable?

An Observable is a Producer of numerous values that “push” them to Observers (Consumers). On invocation, a Function is a lazily evaluated function that synchronously returns a single value.

What is a Promise?

As per the MDN

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value

The difference between Promise and Observable is that Promises can be resolved with a single value or an array of values, and Observables emit one or more values over time.

Angular by default returns Obserable when you invoked HTTP client methods. But let’s assume that you have a function that returns a promise. In this case, you can easily change the return type from Promise to Observable . Converting promise to Observable is very straightforward in Angular. In order to do this first import from operator from ’rxjs and wrap the promise in the from the operator.

import { from } from  'rxjs';
const observable = from(promise);

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post