admin管理员组

文章数量:1405288

From: /@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?

From: https://medium./@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?

Share Improve this question asked Oct 4, 2020 at 12:05 Aquarius_GirlAquarius_Girl 23k71 gold badges249 silver badges441 bronze badges 5
  • No, observables don't "resolve" to anything. – Bergi Commented Oct 4, 2020 at 13:48
  • 2 It is a rubbish article written by someone who doesn't have a clue what they are talking about. – Adrian Brand Commented Oct 4, 2020 at 22:41
  • @adrianbrand please clear the misunderstandings in an answer – Aquarius_Girl Commented Oct 5, 2020 at 1:48
  • @Bergi Please explain the meaning of your statement in an answer. – Aquarius_Girl Commented Oct 5, 2020 at 3:50
  • @Aquarius_Girl I'm not sure what else there is to explain. What is the "resolved result" of an observable? – Bergi Commented Oct 5, 2020 at 9:51
Add a ment  | 

3 Answers 3

Reset to default 4

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.

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./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.

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./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

本文标签: javascriptWhy do we need to convert an Observable to Promise to get a resolved valueStack Overflow