"subscribe()" 中的 "next" 是什么作用?

huangapple go评论62阅读模式
英文:

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) =&gt; {
        &lt;insert code to perform operations with &quot;data&quot;&gt;
    }, error: (err) =&gt; {
		console.error(err);
		// &lt;insert code for what to do on failure&gt;
	}
});

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(&#39;Current Position: &#39;, position);
    },
    error(msg) {
        console.log(&#39;Error Getting Location: &#39;, msg);
    }
});

But I have been just doing it the "normal way", like this (with no "next"):

this.MySubscription = this.myService.getStuff().subscribe((data: any) =&gt; {
    &lt;insert code to perform operations with &quot;data&quot;&gt;
}, error =&gt; {
    console.error(error);
    &lt;insert code for what to do on failure&gt;
});

Is there any functional difference between these three methods of subscribing? How does each method produce different results?

答案1

得分: 5

以下是您要的翻译内容:

  • 第一种方式是,您提供一个带有 next 函数的 PartialObserver&lt;T&gt; 对象,当接收到一个非错误值时,该函数将被执行。
  • 第二种方式也是提供一个带有 next 函数的 PartialObserver&lt;T&gt; 对象,只不过使用了简写的函数语法。
  • 第三种方式是使用 Observable.subscribe(&lt;closure&gt;),这实际上是 Observable.subscribe({ next: &lt;closure&gt; }) 的快捷方式。

通常情况下,您应该使用第三种方式,除非您需要处理 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&lt;T&gt; object with a next function, which will be executed when receiving a value that is not an error
  • In the second one, you give a PartialObserver&lt;T&gt; object with a next function, again, but with the shorthand function syntax
  • In the third one, you use Observable.subscribe(&lt;closure&gt;) which is basically a shortcut to Observable.subscribe({ next: &lt;closure&gt; })

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

这里基本上有三种观察概念的方法,如下所示:

  1. next(): 此方法定义了如何处理由可观察对象发送的数据。

  2. error(): 此方法定义了如何处理错误处理活动。

  3. complete(): 此方法定义了在可观察对象完成生成和发出数据后需要执行的操作。

  4. 在可观察对象发生错误或完成后,无法执行next()方法。

  5. 在取消订阅后,无法调用next()、error()或complete()方法。

  6. 取消订阅在发生错误或完成后被调用,以释放订阅和可观察对象使用的资源。

示例

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.')  
});

因此,您的问题的最终和总结答案是,nextObservable 流中获取最新值。

英文:

There are basically three method of Observable conceptually as below:

  1. next(): this method define that how to process data which is sent by observable

  2. error(): this method define that how to manage error handling activities.

  3. complete(): this method define course of action need to perform after the observable has completed producing and emitting data.

  4. next() method cannot be executed after the observable has errored or completed.

  5. next(), error() or complete() method cannot be called after unsubscribe.

  6. unsubscribe is called on error or complete to free the resources used by the subscription and the observable.

Exmaple

some$.subscribe({  
  next: x =&gt; console.log(&#39;The next value is: &#39;, x),  
  error: err =&gt; console.error(&#39;An error occurred :&#39;, err),  
  complete: () =&gt; console.log(&#39;There are no more action happen.&#39;)  
});

So, Final and summary answer of your question is next get the latest value from the stream of Observable.

huangapple
  • 本文由 发表于 2023年2月14日 22:08:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定