英文:
What does "next" do inside of .subscribe()?
问题
以下是您要翻译的内容:
I have seen three ways to "listen" for changes to a value via an observable / call APIs to fetch data from the backend.
One of these ways has "next:" :
this.MySubscription = this.myService.getStuff().subscribe({
next: (data) => {
<insert code to perform operations with "data">
}, error: (err) => {
console.error(err);
//
}
});
And on the Angular site https://angular.io/guide/observables I see this, with "next(" :
// Call subscribe() to start listening for updates.
const locationsSubscription = locations.subscribe({
next(position) {
console.log('Current Position: ', position);
},
error(msg) {
console.log('Error Getting Location: ', msg);
}
});
But I have been just doing it the "normal way", like this (with no "next"):
this.MySubscription = this.myService.getStuff().subscribe((data: any) => {
<insert code to perform operations with "data">
}, error => {
console.error(error);
});
Is there any functional difference between these three methods of subscribing? How does each method produce different results?
英文:
I have seen three ways to "listen" for changes to a value via an observable / call APIs to fetch data from the backend.
One of these ways has "next:" :
this.MySubscription = this.myService.getStuff().subscribe({
next: (data) => {
<insert code to perform operations with "data">
}, error: (err) => {
console.error(err);
// <insert code for what to do on failure>
}
});
And on the Angular site https://angular.io/guide/observables I see this, with "next(" :
// Call subscribe() to start listening for updates.
const locationsSubscription = locations.subscribe({
next(position) {
console.log('Current Position: ', position);
},
error(msg) {
console.log('Error Getting Location: ', msg);
}
});
But I have been just doing it the "normal way", like this (with no "next"):
this.MySubscription = this.myService.getStuff().subscribe((data: any) => {
<insert code to perform operations with "data">
}, error => {
console.error(error);
<insert code for what to do on failure>
});
Is there any functional difference between these three methods of subscribing? How does each method produce different results?
答案1
得分: 5
以下是您要的翻译内容:
- 第一种方式是,您提供一个带有
next
函数的PartialObserver<T>
对象,当接收到一个非错误值时,该函数将被执行。 - 第二种方式也是提供一个带有
next
函数的PartialObserver<T>
对象,只不过使用了简写的函数语法。 - 第三种方式是使用
Observable.subscribe(<closure>)
,这实际上是Observable.subscribe({ next: <closure> })
的快捷方式。
通常情况下,您应该使用第三种方式,除非您需要处理 error
和/或 complete
的情况。如果必须处理这些情况,您可以在第一种和第二种方式之间选择任何一种,两者都有效,只需在代码中保持一致性(始终使用相同的方式;使用代码检查工具)。
供参考链接:https://rxjs.dev/api/index/class/Observable#subscribe
英文:
The 3 ways you show in your question do the same thing.
It is just 3 different ways to give an observer.
- In the first one, you give a
PartialObserver<T>
object with anext
function, which will be executed when receiving a value that is not an error - In the second one, you give a
PartialObserver<T>
object with anext
function, again, but with the shorthand function syntax - In the third one, you use
Observable.subscribe(<closure>)
which is basically a shortcut toObservable.subscribe({ next: <closure> })
You always use the 3rd way, unless you have to handle error
and/or complete
cases.
If you do have to handle those cases, you have to choose between the 1st or the 2nd way you showed in your question; either one works, just keep consistency in your code (always use the same way in your code; use a linter).
For reference: https://rxjs.dev/api/index/class/Observable#subscribe
答案2
得分: 0
这里基本上有三种观察概念的方法,如下所示:
-
next(): 此方法定义了如何处理由可观察对象发送的数据。
-
error(): 此方法定义了如何处理错误处理活动。
-
complete(): 此方法定义了在可观察对象完成生成和发出数据后需要执行的操作。
-
在可观察对象发生错误或完成后,无法执行next()方法。
-
在取消订阅后,无法调用next()、error()或complete()方法。
-
取消订阅在发生错误或完成后被调用,以释放订阅和可观察对象使用的资源。
示例
some$.subscribe({
next: x => console.log('The next value is: ', x),
error: err => console.error('An error occurred: ', err),
complete: () => console.log('There are no more actions happening.')
});
因此,您的问题的最终和总结答案是,next
从 Observable
流中获取最新值。
英文:
There are basically three method of Observable conceptually as below:
-
next(): this method define that how to process data which is sent by observable
-
error(): this method define that how to manage error handling activities.
-
complete(): this method define course of action need to perform after the observable has completed producing and emitting data.
-
next() method cannot be executed after the observable has errored or completed.
-
next(), error() or complete() method cannot be called after unsubscribe.
-
unsubscribe is called on error or complete to free the resources used by the subscription and the observable.
Exmaple
some$.subscribe({
next: x => console.log('The next value is: ', x),
error: err => console.error('An error occurred :', err),
complete: () => console.log('There are no more action happen.')
});
So, Final and summary answer of your question is next
get the latest value from the stream of Observable
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论