英文:
Observables in Angular for http calls
问题
我正在查看上述的Angular可观察对象文档,在其中一节中,他们将承诺与可观察对象进行了比较,并提到了以下观点:
可观察对象提供多个值。承诺提供一个值。这使得可观察对象在随时间获取多个值时非常有用。
所以我在尝试形成可观察对象的心理模型时感到困惑。当他们说可观察对象提供多个值时,这是什么意思?
比如说,我进行了一个HTTP调用,可观察对象显然会返回特定的数据(响应)。即使承诺也会执行同样的操作。
而且,随着时间的推移,API可能会返回响应,也可能会失败,这在承诺中也可以实现。
那么,在使用可观察对象和承诺进行HTTP调用时的主要区别是什么?
除了承诺会立即执行,而可观察对象需要被订阅之外,还有什么不同?
英文:
Angular Observable Documentation
I was going through above angular observable documentation and in one section where they compare promises with observalbe the following point is give
> Observables provide many values. Promises provide one. This makes observables useful for getting multiple values over time.
So I am trying here to make a mental model of Observable and getting confused
So when they say Observable provide many values what does it mean
Like suppose i do a http call and observable will obviously return particular data(response)
Even promises will do same
And overtime api can either return with response or api might fail ,the same can done with promise as well
So whats the major difference while doing a http call using observable and promise
Apart from Promise executes immediately and Observable needs to be subscribed
答案1
得分: 2
这是你要翻译的部分:
"这是因为你考虑的是一个错误的例子。HTTP调用在定义上是一个单一的请求-响应周期。
相反,考虑一下像鼠标点击这样的事件。
假设你有一个鼠标点击观察者。你可以在每次鼠标点击时执行你的逻辑。在JavaScript世界中,我们实际上不需要观察者模式,因为事件处理程序在该语言中是正常的事情(但它们看起来和感觉像观察者模式,没有设计模式的样板代码)。所以假设你想在每次点击按钮时播放小号声音。你只需这样做:
button.on('click', () => play_trumpet_sound());
另一方面,Promise只能发生一次。因此,如果按钮公开了Promise接口,你需要做类似这样的事情:
async function listenToClick() {
while (1) {
await button.getClick();
play_trumpet_sound();
}
}
listenToClick();
虽然Promises(以及伴随它们的async/await
语法)对于许多用例来说更简洁且易于使用,但并非所有用例都适合Promises。当你需要监听可能再次发生的事件时,最好使用常规回调(或在其他语言中使用观察者模式)。"
英文:
It's because you're thinking about a wrong example. A HTTP call is by definition a single request-response cycle.
Consider instead something like mouse click.
Suppose you have a mouse click observer. You can execute your logic on every mouse click. In the javascript world we don't really need an observer pattern since event handlers are normal things in the language (they do however look and feel like the observer pattern without the boilerplate of a design pattern). So suppose you want to play a trumpet sound every time you click a button. You'd just do something like:
button.on('click', () => play_trumpet_sound());
On the other hand, a Promise can only happen once. So if the button exposes a Promise interface you'd instead need to do something like:
async function listenToClick () {
while (1) {
await button.getClick();
play_trumpet_sound();
}
}
listenToClick();
While Promises (and the async/await
syntax that accompany them) are more concise and easier to use for a lot of use-cases, not all use-cases are better with Promises. When you need to listen for something that can happen again you're better off with regular callbacks (or in other languages the Observer pattern)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论