简体   繁体   中英

Why do we need to convert an Observable to Promise to get a resolved value?

From: https://medium.com/@ole.ersoy/synchronous-programming-with-rxjs-observable-6276721e674a

Scenario

We want to return a resolved value from our Observable.
Approach 1

Turn the Observable into a Promise and await it like this:

async function returnTrue()
{
return await of(true).toPromise();
}
//logs true
returnTrue().then(console.log);

The return value is now guaranteed to be available, as long as the observable did not error out.

Why do we need to turn an Observable to a Promise to get a resolved result? Observables are not capable on their own to get resolved results?

What's the point that I am missing here?

There's an incorrect statement in that article, it is said that Synchronous Programming with RxJS Observable but using Promise as an example.

However, Promise is always asynchronous even if it's immediately resolved . That's why we need async/await , then or callback for executing a promise.

It's no need to convert Observable to Promise to get a value because observable has a subscribe function.

Also noted that Promise can only return a single value , meanwhile Observable can return multiple values .

More example here: https://rxjs-dev.firebaseapp.com/guide/observable

In my experience using Angular where Observable is heavily used, a use case that I need to convert it to Promise is when I need to pass data to 3rd party library that accepts Promise as its parameter.

It is a rubbish article, the rubbish is in the title, "Synchronous Programming with RxJS Observable". The only reason that any of this is synchronous is because the author is using of. If any of these observable were useful observables they would be asynchronous. This article can basically be ignored and flagged as pointless.

Actually there are two ways to run an Observable , depending on the Requirement

  1. Convert to a Promise
  2. Call the subscribe method on them

Why it is required?

  • Because Observable are like functions , as we define the function and when we require to run the function we call them

  • Similarly we create Observable and when we require to run( resolve ) them we either

    • Convert it to a Promise
    • Call the Subscribe method on them
  • Also from the below example we can see the difference between the 2 methods to resolve the Observable .

 const Observable1 = rxjs.of(1, 2, 3); // By calling subscribe method Observable1.subscribe(next=>{ console.log("A", next); }); // By converting to Promise (async()=>{ const result = await Observable1.toPromise() console.log("B", result) })()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.1.0/rxjs.umd.js"></script>

Reference:

  1. https://rxjs.dev/guide/overview
  2. https://rxjs.dev/api/index/function/of

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM