Aurelia HttpClient取消请求 [英] Aurelia HttpClient cancel requests
问题描述
我正在尝试构建一个自动完成组件,并希望使其在输入时取消对服务器的未解决请求.
I am trying to build an auto complete component and want to make it cancel unresolved requests to the server while they type.
在HttpClient的文档中找不到与此相关的文档.它提到它是可取消的(与fetch不同),但不是. https://aurelia.io/docs/plugins/http-services
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;
}
}
在哪里可以找到有关如何提出可取消请求的更多信息?我的Google-fu使我失败了.
Where can I find out more information on how to make cancellable requests? My google-fu is failing me.
推荐答案
您可以执行以下操作:
this.client["pendingRequests"].forEach(request => {
request.abort();
});
我必须做["pendingRequests"]
,因为我正在使用TypeScript,并且数组似乎不在定义之内.
I am having to do ["pendingRequests"]
as I'm using TypeScript and the array does not seem to be within the definition.
注意:我还针对每个自动完成功能使用了范围限定的HttpClient,因此,当它取消所有先前的请求时,不会意外取消该应用程序正在请求的其他内容.
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.
这篇关于Aurelia HttpClient取消请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!