代码之家  ›  专栏  ›  技术社区  ›  4imble

Aurelia HttpClient取消请求

  •  2
  • 4imble  · 技术社区  · 7 年前

    我在HttpClient的文档中找不到与此相关的文档。它提到它是可取消的(与fetch不同),但没有提到如何取消。 https://aurelia.io/docs/plugins/http-services

    目前,我有这个我拼凑起来相当盲目,毫不奇怪,它甚至没有中止请求:

    async searchTermChanged(newValue, oldValue) {   
        if (newValue.length < 3)
          return;
    
        if (this.promises.length) {
           this.promises.forEach(x => x.abort());
           //should probably remove too
        }
    
        var promise = this.httpClient.post(this.endpoint, { SearchTerm: newValue });
        this.promises.push(promise);
    
        var data = await promise;
    
        var response = JSON.parse(data.response);
    
        this.results = response;
      }
    }
    

    在哪里可以找到有关如何提出可取消请求的更多信息?我的googlefu让我失望了。

    1 回复  |  直到 7 年前
        1
  •  2
  •   4imble    7 年前

    this.client["pendingRequests"].forEach(request => {
       request.abort();
    });
    

    我不得不做 ["pendingRequests"] 因为我使用的是TypeScript,数组似乎不在定义范围内。

    注意:我还在每个autocomplete中使用一个作用域HttpClient,这样当它取消所有以前的请求时,它不会意外地取消应用程序正在请求的其他东西。

    推荐文章