thaerlabs / axios-cancel Goto Github PK
View Code? Open in Web Editor NEWSimplify cancellation of http requests when using the awesome axios library
Simplify cancellation of http requests when using the awesome axios library
When using axios-cancel and http response is 401 error (I think the same for all 4xx) "Potentially unhandled rejection [15] Error" occurs.
The problem that error does not get to the .catch():
return axios({
url: prepareURL(url),
method: 'post',
headers: prepareAuthHeader(),
data: params,
requestId: requestId
}).then((response) => response.data)
.catch(function(thrown){ // 400 Error is not going here
if (axios.isCancel(thrown)){
console.log('Request canceled', thrown.message);
} else{
when.reject(thrown);
}
});
axio-cancel debug log:
adding request 1
index.js:1 adding request '1484560946801' xhr.js:175 POST http://..../... 401 (Unauthorized) dispatchXhrRequest @ xhr.js:175 xhrAdapter @ xhr.js:12 dispatchRequest @ dispatchRequest.js:52 unhandledRejection.js:23 Potentially unhandled rejection [1] Error: Request failed with status code 401 at createError (http://..../index-1484560715206.js:24790:16) at settle (http://..../index-1484560715206.js:24762:13) at XMLHttpRequest.handleLoad (http://..../index-1484560715206.js:24638:8) (anonymous) @ unhandledRejection.js:23 report @ unhandledRejection.js:50 flush @ unhandledRejection.js:72
index.js:1 removing request 1
P.S. I'm new to javascript so maybe it could be my mistake, I could add more context if needed.
how to clear a particular request
is there any other library which is in maintained state ?
Currently Cancel Exception contains only string for the reason.
If can refer to config object from exception,
that would be great feature to handle after cancellation works.
I was wondering how you would add typings for this, since Axios comes with its own. I can't think how to override the axios object and add the properties like requestId.
Axios#isCancel implementation ==> https://github.com/axios/axios/blob/master/lib/cancel/isCancel.js
Thrown cancellation error object when using the library with axios:
Notice how the implementation doesn't account for the axios-cancel thrown error object format.
Example used in documentation:
// Single request cancellation
const requestId = 'my_sample_request';
const promise = axios.get(url, {
requestId: requestId
})
.then((res) => {
console.log('resolved');
}).catch((thrown) => {
if (axios.isCancel(thrown)) { /// DOESN't WORK
console.log('request cancelled');
} else {
console.log('some other reason');
}
});
axios.cancel(requestId);
While cancellation of request Nuxtjs throws error as below:
cancelRequest(requestId)
from RequestManager.addRequest
. Found duplicate pending request.
Any help for this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.