Angular Promise – debounceTime 行为

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

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 &#39;@angular/http&#39;;
   

    public httpService: Http;
    (....)

    public callAction(): Promise&lt;void&gt; {
	    let r = this.httpService.post(url, panelData);

        return r.toPromise().then((response: { json: () =&gt; any; }) =&gt; {
            if (response.json()) {
                this.processResponse(panel, response.json());
            }
        }).catch((reason: any) =&gt; {
            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 &#39;@angular/http&#39;;


public httpService: Http;
(....)

let r = this.httpService.post(url, panelData).debounceTime(5000);

public callAction(): Promise&lt;void&gt; {
    return this.r.toPromise().then((response: { json: () =&gt; any; }) =&gt; {
        if (response.json()) {
            this.processResponse(panel, response.json());
        }
    }).catch((reason: any) =&gt; {
        throw reason;
    });
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 17:51:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576358.html
匿名

发表评论

匿名网友

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

确定