Javascript 我如何在 Rx Observable 上“等待”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34190375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:01:46  来源:igfitidea点击:

How can I `await` on an Rx Observable?

javascriptrxjsecmascript-7

提问by CheapSteaks

I'd like to be able to await on an observable, e.g.

我希望能够等待一个可观察的对象,例如

const source = Rx.Observable.create(/* ... */)
//...
await source;

A naive attempt results in the await resolving immediately and not blocking execution

天真的尝试导致等待立即解决而不阻止执行

Edit: The pseudocode for my full intended use case is:

编辑:我的完整预期用例的伪代码是:

if (condition) {
  await observable;
}
// a bunch of other code

I understand that I can move the other code into another separate function and pass it into the subscribe callback, but I'm hoping to be able to avoid that.

我知道我可以将其他代码移动到另一个单独的函数中并将其传递给订阅回调,但我希望能够避免这种情况。

回答by Macil

You have to pass a promise to await. Convert the observable's next event to a promise and await that.

您必须将承诺传递给await. 将 observable 的下一个事件转换为 promise 并等待它。

if (condition) {
  await observable.first().toPromise();
}

Edit note: This answer originally used .take(1) but was changed to use .first() which avoids the issue of the Promise never resolving if the stream ends before a value comes through.

编辑说明:此答案最初使用 .take(1) 但已更改为使用 .first() 这避免了如果流在值通过之前结束则无法解决 Promise 的问题。

回答by Estus Flask

It likely has to be

它可能必须是

await observable.first().toPromise();

As it was noted in comments before, there is substantial difference between take(1)and first()operators when there is empty completed observable.

正如之前评论中所指出的,当有空的已完成可观察对象时,take(1)first()运算符之间存在重大差异。

Observable.empty().first().toPromise()will result in rejection with EmptyErrorthat can be handled accordingly, because there really was no value.

Observable.empty().first().toPromise()会导致拒收,EmptyError表示可以相应处理,因为真的没有价值。

And Observable.empty().take(1).toPromise()will result in resolution with undefinedvalue.

而且Observable.empty().take(1).toPromise()会导致分辨率undefined值。

回答by Josh Durham

You will need to awaita promise, so you will want to use toPromise(). See thisfor more details on toPromise().

你需要await一个承诺,所以你会想要使用toPromise(). 有关更多详细信息,请参阅内容toPromise()

回答by Emerica

If toPromiseis deprecated for you, you can use .pipe(take(1)).toPromisebut as you can see hereit's not deprecated.

如果toPromise不推荐使用,您可以使用,.pipe(take(1)).toPromise但正如您在此处看到的那样它没有被弃用。

So please juste use toPromise(RxJs 6) as said:

所以请使用toPromise(RxJs 6) 说:

//return basic observable
const sample = val => Rx.Observable.of(val).delay(5000);
//convert basic observable to promise
const example = sample('First Example')
  .toPromise()
  //output: 'First Example'
  .then(result => {
    console.log('From Promise:', result);
  });

async/await example:

异步/等待示例:

//return basic observable
const sample = val => Rx.Observable.of(val).delay(5000);
//convert basic observable to promise
const example = await sample('First Example').toPromise()
// output: 'First Example'
console.log('From Promise:', result);

Read more here.

在这里阅读更多。

And please remove this wrong claim saying toPromiseis deprecated.

并且请删除这个错误的声明说toPromise已弃用。