【问题标题】:How do I abort an Ionic promise如何中止 Ionic 承诺
【发布时间】:2019-06-15 23:32:58
【问题描述】:

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

如何中止 Promise 以便创建新 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


【解决方案1】:

在 Ionic 中,您可以使用 rxjs switchMap 运算符和 debounceTime 运算符非常轻松地做到这一点。

example code

【讨论】:

  • 但是我用ionChange 打电话给te listVendas 我在哪里创建去抖动?
【解决方案2】:

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

    <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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2017-10-13
    • 2018-08-18
    • 2016-06-01
    • 1970-01-01
    • 2014-11-16
    相关资源
    最近更新 更多