中止所有axios待处理的请求并创建新的

时间:2019-11-07 13:32:24

标签: javascript node.js angular typescript axios

我正在使用axios进行API请求,因此我想中止所有正在运行/待处理的请求,并使用其他API创建新请求。

尝试了以下代码

async getOldResponse() {
    const response_old: any = await this.axios.post("/search_old", this.searchData);
    console.log(response_old)
}
async getOldOtherResponse() {
    const response_old_second: any = await this.axios.post("/search_old_second", this.searchData);
    console.log(response_old_second);
}
async getNewSearch() {
    // here i want to cancel all pending requests.
    const CancelToken = this.axios.CancelToken;
    const source = CancelToken.source();
    source.cancel('All previous pending request cancelled');
    const response: any = await this.axios.post("/search_new", this.searchData);
    console.log(response);
}
ngOnInit() {
    this.getOldResponse();
    this.getOldOtherResponse();
    this.getNewSearch();
}

基本上我想中止/search_oldsearch_old_second API请求并创建search_new

2 个答案:

答案 0 :(得分:0)

您可以使用cancellation

const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.get('/user/12345', {
  cancelToken: source.token
}).catch(function (thrown) {
  if (axios.isCancel(thrown)) {
    console.log('Request canceled', thrown.message);
  } else {
    // handle error
  }
});

axios.post('/user/12345', {
  name: 'new name'
}, {
  cancelToken: source.token
})

// cancel the request (the message parameter is optional)
source.cancel('Operation canceled by the user.');

答案 1 :(得分:0)

对此我不确定,但是从我记得的情况来看,会是这样。

取自axios文档https://github.com/axios/axios

---------------------------------------------------------------------------
PicklingError                             Traceback (most recent call last)
<ipython-input-64-ea578c0689fa> in <module>
----> 1 v.map(lambda x: time_algorithm(optimized_hcs, x, h1), states)

<d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\decorator.py:decorator-gen-142> in map(self, f, *sequences, **kwargs)

d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\ipyparallel\client\view.py in sync_results(f, self, *args, **kwargs)
     50     self._in_sync_results = True
     51     try:
---> 52         ret = f(self, *args, **kwargs)
     53     finally:
     54         self._in_sync_results = False

d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\ipyparallel\client\view.py in map(self, f, *sequences, **kwargs)
    621         assert len(sequences) > 0, "must have some sequences to map onto!"
    622         pf = ParallelFunction(self, f, block=block, **kwargs)
--> 623         return pf.map(*sequences)
    624 
    625     @sync_results

d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\ipyparallel\client\remotefunction.py in map(self, *sequences)
    297         and mismatched sequence lengths will be padded with None.
    298         """
--> 299         return self(*sequences, __ipp_mapping=True)
    300 
    301 __all__ = ['remote', 'parallel', 'RemoteFunction', 'ParallelFunction']

<d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\decorator.py:decorator-gen-132> in __call__(self, *sequences, **kwargs)

d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\ipyparallel\client\remotefunction.py in sync_view_results(f, self, *args, **kwargs)
     78     view = self.view
     79     if view._in_sync_results:
---> 80         return f(self, *args, **kwargs)
     81     view._in_sync_results = True
     82     try:

d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\ipyparallel\client\remotefunction.py in __call__(self, *sequences, **kwargs)
    261             if sum([len(arg) for arg in args]) == 0:
    262                 continue
--> 263             args = [PrePickled(arg) for arg in args]
    264 
    265             if _mapping:

d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\ipyparallel\client\remotefunction.py in <listcomp>(.0)
    261             if sum([len(arg) for arg in args]) == 0:
    262                 continue
--> 263             args = [PrePickled(arg) for arg in args]
    264 
    265             if _mapping:

d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\ipyparallel\serialize\serialize.py in __init__(self, obj)
     42     """
     43     def __init__(self, obj):
---> 44         self.buffers = serialize_object(obj)
     45 
     46 

d:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages\ipyparallel\serialize\serialize.py in serialize_object(obj, buffer_threshold, item_threshold)
    123         buffers.extend(_extract_buffers(cobj, buffer_threshold))
    124 
--> 125     buffers.insert(0, pickle.dumps(cobj, PICKLE_PROTOCOL))
    126     return buffers
    127 

PicklingError: Can't pickle <class '__main__.Client'>: it's not the same object as __main__.Client
相关问题