英文:
Angular Promise - debounceTime behavior
问题
我正在使用Angular 7,并且我有以下(正常工作的)代码。
import { Http } from '@angular/http';
public httpService: Http;
(...)
public callAction(): Promise<void> {
let r = this.httpService.post(url, panelData);
return r.toPromise().then((response: { json: () => any; }) => {
if (response.json()) {
this.processResponse(panel, response.json());
}
}).catch((reason: any) => {
throw reason;
});
}
方法this.httpService.post
返回一个Observable<Response>
。
我试图避免多次服务器调用,为此我尝试使用防抖行为。
我在我的Observable<Response>
上添加了debounceTime
,但当调用Promise时它不起作用。
let r = this.httpService.post(url, panelData).debounceTime(5000);
return r.toPromise().then()
我知道我没有订阅Observable,但当我调用toPromise()
时,这个行为应该被传递给Promise吗?
PS- 我也尝试使用管道:
let r = this.httpService.post(url, panelData).pipe(debounceTime(5000));
英文:
I'm using angular 7 and I've the following (working) code.
import { Http } from '@angular/http';
public httpService: Http;
(....)
public callAction(): Promise<void> {
let r = this.httpService.post(url, panelData);
return r.toPromise().then((response: { json: () => any; }) => {
if (response.json()) {
this.processResponse(panel, response.json());
}
}).catch((reason: any) => {
throw reason;
});
}
The method this.httpService.post returns an Observable< Response >.
I'm trying to avoid multiple server calls and for that I'm trying to use the debounce behavior.
I added the debounceTime on my Observable<Response> but it doesn't work when the Promise is called.
let r = this.httpService.post(url, panelData).debounceTime(5000);
return r.toPromise().then()
I know that I'm not subscribing the Observable but when I call the toPromise() should this behavior be "imported" to the promise?
PS- I also try with a pipe
let r = this.httpService.post(url, panelData).pipe(debounceTime(5000));
答案1
得分: 2
你需要将let r = this.httpService.post(url, panelData).debounceTime(5000);
放在你的callAction()
函数外部,而是作为类的一个单独的函数/属性存在。发生的情况是每次调用callAction()
函数时都会创建一个新的防抖观察者r
,而你需要每次调用同一个:
import { Http } from '@angular/http';
public httpService: Http;
(...)
let r = this.httpService.post(url, panelData).debounceTime(5000);
public callAction(): Promise<void> {
return this.r.toPromise().then((response: { json: () => any; }) => {
if (response.json()) {
this.processResponse(panel, response.json());
}
}).catch((reason: any) => {
throw reason;
});
}
英文:
You need to have let r = this.httpService.post(url, panelData).debounceTime(5000);
outside your callAction()
function and instead sit as a separate function/property of the class.
What is occurring is every time callAction()
function is called a new debounce observable r
is being created, instead you need to call the same one each time:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { Http } from '@angular/http';
public httpService: Http;
(....)
let r = this.httpService.post(url, panelData).debounceTime(5000);
public callAction(): Promise<void> {
return this.r.toPromise().then((response: { json: () => any; }) => {
if (response.json()) {
this.processResponse(panel, response.json());
}
}).catch((reason: any) => {
throw reason;
});
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论