S
S
Sergei Abramov2019-11-06 22:33:41
Angular
Sergei Abramov, 2019-11-06 22:33:41

How to abort a request?

I use ng-select, following the example from the docs ( Demo, bottom example ), I did a search through the backend:

private loadCountry() {
        this.countryList$ = concat(
            of([]), // default items
            this.countryInput$.pipe(
                debounceTime(this.debounceTime),
                distinctUntilChanged(),
                tap(() => this.countryLoading = true),
                switchMap(search => {
                    return this.locationService.getCountry(search).pipe(
                        catchError(() => of([])),  // empty list on error
                        tap(() => this.countryLoading = false)
                    ).toPromise().then(data => {
                        const countries = [];
                        const cnt = data.body.length;
                        for (let i = 0; i < cnt; i++) {
                            countries.push(new Country(data.body[i]));
                        }
                        return countries;
                    });
                }),
            )
        );
    }

But there is a problem: after the user selects the desired item in the select, this method will work again, but with search = null. Here's how to avoid it. What is the right way to abort the execution of this method when search = null?
I did this, but it seems to me that this is not technically correct, and there are more correct solutions.
private loadCountry() {
        this.countryList$ = concat(
            of([]), // default items
            this.countryInput$.pipe(
                debounceTime(this.debounceTime),
                distinctUntilChanged(),
                tap(() => this.countryLoading = true),
                switchMap(search => {
                    
                    if (!search) {this.countryLoading = false; return []; }  // Мое решение
                    
                    return this.locationService.getCountry(search).pipe(
                        catchError(() => of([])), // empty list on error
                        tap(() => this.countryLoading = false)
                    ).toPromise().then(data => {
                        const countries = [];
                        const cnt = data.body.length;
                        for (let i = 0; i < cnt; i++) {
                            countries.push(new Country(data.body[i]));
                        }
                        return countries;
                    });
                }),
            )
        );
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Terentiev, 2019-11-07
@PatriotSY

In the pipe, use the filter rxjs operator and filter out null values.
https://www.learnrxjs.io/operators/filtering/filte...

A
Anton Shvets, 2019-11-07
@Xuxicheta

And in the example, the input parameter [minTermLength]="2"does not tell you anything?
and remove the promise, it's a nightmare

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question