Angular: how to return the first successful response from the list of http requests

Discussão em 'Angular' iniciado por SimonD, Janeiro 2, 2025.

  1. SimonD

    SimonD Guest

    I have a list of servers urls and making sequential http requests to them in a loop. When the success response arrives from the current request I want to break the loop and not to call all other servers. Could someone advice me how this could be handled in Angular/RxJS? Something like:

    getClientData() {
    for(let server of this.httpsServersList) {
    var myObservable = this.queryData(server)
    .pipe(
    map((response: any) => {
    const data = (response || '').trim();

    if(data && this.dataIsCorrect(data)) {
    return data; // **here I want to break from the loop!**
    }
    })
    );
    return myObservable;
    }
    }

    private queryData(url: string) {
    return this.http.get(url, { responseType: 'text' });
    }

    Continue reading...

Compartilhe esta Página