我如何中止Ionic的承诺

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

我正在制作一个项目列表,每次用户输入一些值,它会调用在API中立即搜索的listVendas函数,但是这会导致一堆请求,有些请求可以在其他请求之前完成,所以我的问题是。

我如何中止Promise以便创建一个新的?

listVendas(event?: any) {
    let codigovenda;

    if (event) {
        codigovenda = event.value;
    }

    if (typeof this.promise != 'undefined') {
        // HOW DO I ABORT THE PREVIOUS PROMISE
    }

    this.promise = this.vendaProvider.getAll(codigovenda);

    this.promise.then(data => {
        this.vendas = data;
    })
}
ionic-framework ionic3 angular-promise
2个回答
1
投票

在Ionic中,您可以使用rxjs switchMap运算符和debounceTime运算符来轻松完成此操作。

example code


0
投票

我搜索了很多,你不能中止一个承诺,所以我的解决方案是每次用户输入内容时不要发出请求,而不是我创建了一个按钮来发出请求

    <form [formGroup]="searchForm" (ngSubmit)="listVendas()">
        <ion-card class="search">
            <ion-icon class="icon-search" name="search"></ion-icon>
            <ion-input type="number" pattern="[0-9]*" value="" formControlName="codigovenda"></ion-input>
            <ion-icon class="icon-camera" name="camera" (click)="scanBacode()"></ion-icon>
        </ion-card>
        <ion-row class="row-submit" justify-content-center>
            <button ion-button full icon-start type="submit">
                Buscar
            </button>
        </ion-row>
    </form>
© www.soinside.com 2019 - 2024. All rights reserved.