简体   繁体   English

为什么我们需要将 Observable 转换为 Promise 以获得已解析的值?

[英]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来自: https : //medium.com/@ole.ersoy/synchronous-programming-with-rxjs-observable-6276721e674a

Scenario设想

We want to return a resolved value from our Observable.我们想从 Observable 返回一个已解析的值。
Approach 1方法一

Turn the Observable into a Promise and await it like this:将 Observable 变成 Promise 并像这样等待它:

async function returnTrue()异步函数 returnTrue()
{ {
return await of(true).toPromise(); return await of(true).toPromise();
} }
//logs true //记录为真
returnTrue().then(console.log); returnTrue().then(console.log);

The return value is now guaranteed to be available, as long as the observable did not error out.现在保证返回值可用,只要 observable 没有出错。

Why do we need to turn an Observable to a Promise to get a resolved result?为什么我们需要将 Observable 转换为 Promise 以获得已解决的结果? Observables are not capable on their own to get resolved results? Observables 不能自己获得已解决的结果?

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.那篇文章有一个错误的说法,说是Synchronous Programming with RxJS Observable但以Promise为例。

However, Promise is always asynchronous even if it's immediately resolved .然而, Promise总是异步的,即使它立即被解决 That's why we need async/await , then or callback for executing a promise.这就是为什么我们需要async/awaitthencallback来执行承诺。

It's no need to convert Observable to Promise to get a value because observable has a subscribe function.不需要将 Observable 转换为 Promise 来获取值,因为 observable 具有subscribe功能。

Also noted that Promise can only return a single value , meanwhile Observable can return multiple values .还要注意Promise只能返回一个值,而 Observable可以返回多个值

More example here: https://rxjs-dev.firebaseapp.com/guide/observable更多示例: 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.根据我在大量使用 Observable 的情况下使用 Angular 的经验,我需要将其转换为 Promise 的用例是当我需要将数据传递给接受 Promise 作为其参数的 3rd 方库时。

It is a rubbish article, the rubbish is in the title, "Synchronous Programming with RxJS Observable".这是一篇垃圾文章,垃圾就在标题“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.如果这些 observable 中的任何一个是有用的 observable,它们将是异步的。 This article can basically be ignored and flagged as pointless.这篇文章基本上可以被忽略并标记为毫无意义。

Actually there are two ways to run an Observable , depending on the Requirement实际上有两种方法可以运行Observable ,具体取决于需求

  1. Convert to a Promise转换为Promise
  2. Call the subscribe method on them对它们调用subscribe方法

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因为Observable就像functions ,当我们定义函数时,当我们需要运行函数时,我们调用它们

  • Similarly we create Observable and when we require to run( resolve ) them we either类似地,我们创建了Observable ,当我们需要运行(解析)它们时,我们要么

    • Convert it to a Promise将其转换为Promise
    • Call the Subscribe method on them对它们调用Subscribe方法
  • Also from the below example we can see the difference between the 2 methods to resolve the Observable .同样从下面的示例中,我们可以看到解决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 https://rxjs.dev/guide/overview
  2. https://rxjs.dev/api/index/function/of https://rxjs.dev/api/index/function/of

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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