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
How can I `await` on an Rx Observable?
提问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 EmptyError
that 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 undefined
value.
而且Observable.empty().take(1).toPromise()
会导致分辨率undefined
值。
回答by Josh Durham
回答by Emerica
If toPromise
is deprecated for you, you can use .pipe(take(1)).toPromise
but 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 toPromise
is deprecated.
并且请删除这个错误的声明说toPromise
已弃用。