Aurelia HttpClient cancel requests

折月煮酒 提交于 2019-12-12 19:25:12

问题


I am trying to build an auto complete component and want to make it cancel unresolved requests to the server while they type.

I can find no documentation around this in the documentation for HttpClient. It mentions it IS cancellable (unlike fetch) but not how. https://aurelia.io/docs/plugins/http-services

Currently I have this which I cobbled together quite blindly, unsurprisingly it doesn't even abort the requests:

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

Where can I find out more information on how to make cancellable requests? My google-fu is failing me.


回答1:


Looks like you can do this:

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

I am having to do ["pendingRequests"] as I'm using TypeScript and the array does not seem to be within the definition.

Note: I am also using a scoped HttpClient per autocomplete, so that when it cancels all previous requests it will not accidentally cancel something else that the app is requesting.



来源:https://stackoverflow.com/questions/52446271/aurelia-httpclient-cancel-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!