0
votes

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?

3
No, observables don't "resolve" to anything.Bergi
It is a rubbish article written by someone who doesn't have a clue what they are talking about.Adrian Brand
@adrianbrand please clear the misunderstandings in an answerAquarius_Girl
@Bergi Please explain the meaning of your statement in an answer.Aquarius_Girl
@Aquarius_Girl I'm not sure what else there is to explain. What is the "resolved result" of an observable?Bergi

3 Answers

2
votes

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.

2
votes

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.

1
votes

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