Angular Promise-debounceTime行为

问题描述 投票:0回答:1

我正在使用角度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

我正在尝试避免多次服务器调用,为此,我正在尝试使用debounce behavior

我在Observable上添加了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));
angular angular-promise debounce
1个回答
0
投票

您需要在let r = this.httpService.post(url, panelData).debounceTime(5000);函数之外使用callAction(),而应作为该类的单独函数/属性来使用。发生的是每次调用callAction()函数都会创建一个新的可观察到的反跳r,而您每次都需要调用相同的防抖:

© www.soinside.com 2019 - 2024. All rights reserved.