我正在制作一个项目列表,每次用户输入一些值,它会调用在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中,您可以使用rxjs switchMap运算符和debounceTime运算符来轻松完成此操作。
我搜索了很多,你不能中止一个承诺,所以我的解决方案是每次用户输入内容时不要发出请求,而不是我创建了一个按钮来发出请求
<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>