Cancelling ongoing http requests when next one is started - web app search feature
up vote
3
down vote
favorite
This piece of code is aimed to cancel any ongoing search requests sent from the web application if next request is started, e.g. after user updates the search term.
I tested it in the final application and everything looks fine. It has already been deployed to the remote environment in our organization.
It appears to work fine, but are there any pitfalls in the code?
const ongoingRequests = ;
const fetchData = (text, activePage, itemsPerPage) => async dispatch => {
dispatch({ type: FETCH_ENTITIES });
const page = activePage - 1;
let response;
// cancel any ongoing search requests
if (ongoingRequests.length > 0) {
ongoingRequests.map(x => x.cancel('next request started'));
ongoingRequests.length = 0;
}
// start a new request
response = await (async () => {
const cts = axios.CancelToken.source();
ongoingRequests.push(cts);
let path =
text !== ''
? `/Search?PageNumber=${page}&PageSize=${itemsPerPage}&SearchTerm=${text}`
: `?QueryLead=true&PageNumber=${page}&PageSize=${itemsPerPage}`;
return axios
.get(`${env_urls.api.entity}Entity${path}`, {
cancelToken: cts.token,
})
.then(response => {
return response;
})
.catch(error => {
if (
typeof cts !== 'undefined' &&
typeof cts.token !== 'undefined' &&
typeof cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
// request cancelled, everything ok
} else {
throw error;
}
});
})();
// response is undefined if the request has been cancelled
if (typeof response !== 'undefined') {
const data = response.data;
dispatch({ type: FETCH_ENTITIES_SUCCESS, data });
}
};
javascript beginner axios
New contributor
add a comment |
up vote
3
down vote
favorite
This piece of code is aimed to cancel any ongoing search requests sent from the web application if next request is started, e.g. after user updates the search term.
I tested it in the final application and everything looks fine. It has already been deployed to the remote environment in our organization.
It appears to work fine, but are there any pitfalls in the code?
const ongoingRequests = ;
const fetchData = (text, activePage, itemsPerPage) => async dispatch => {
dispatch({ type: FETCH_ENTITIES });
const page = activePage - 1;
let response;
// cancel any ongoing search requests
if (ongoingRequests.length > 0) {
ongoingRequests.map(x => x.cancel('next request started'));
ongoingRequests.length = 0;
}
// start a new request
response = await (async () => {
const cts = axios.CancelToken.source();
ongoingRequests.push(cts);
let path =
text !== ''
? `/Search?PageNumber=${page}&PageSize=${itemsPerPage}&SearchTerm=${text}`
: `?QueryLead=true&PageNumber=${page}&PageSize=${itemsPerPage}`;
return axios
.get(`${env_urls.api.entity}Entity${path}`, {
cancelToken: cts.token,
})
.then(response => {
return response;
})
.catch(error => {
if (
typeof cts !== 'undefined' &&
typeof cts.token !== 'undefined' &&
typeof cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
// request cancelled, everything ok
} else {
throw error;
}
});
})();
// response is undefined if the request has been cancelled
if (typeof response !== 'undefined') {
const data = response.data;
dispatch({ type: FETCH_ENTITIES_SUCCESS, data });
}
};
javascript beginner axios
New contributor
1
"Does it work as intended?" : it's up to you to tell us :]
– Calak
2 days ago
@Calak Everything is fine when I test it in the application but maybe there is an issue that I cannot see. Like some js construct that makes no sense ;)
– michal
2 days ago
"Does it work as intended?" Did you test it? One of the prerequisites of posting on Code Review is that the code has to work to the best of your knowledge. Does it? You say it works in the application. Is that the final application that needs it? A test application?
– Mast
2 days ago
1
@Mast I have updated the question.
– michal
2 days ago
1
When I implement this type of search functionality I usually cancel the current request when the user presses a key (on the keypress, paste events) and start a timer for a fraction of a second. I only start the request when the timer expires. That way I don't start requests while the user is still typing. I've also rarely found it unnecessary to keep an array of requests. I just keep the currently executing one if any.
– Marc Rohloff
2 days ago
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
This piece of code is aimed to cancel any ongoing search requests sent from the web application if next request is started, e.g. after user updates the search term.
I tested it in the final application and everything looks fine. It has already been deployed to the remote environment in our organization.
It appears to work fine, but are there any pitfalls in the code?
const ongoingRequests = ;
const fetchData = (text, activePage, itemsPerPage) => async dispatch => {
dispatch({ type: FETCH_ENTITIES });
const page = activePage - 1;
let response;
// cancel any ongoing search requests
if (ongoingRequests.length > 0) {
ongoingRequests.map(x => x.cancel('next request started'));
ongoingRequests.length = 0;
}
// start a new request
response = await (async () => {
const cts = axios.CancelToken.source();
ongoingRequests.push(cts);
let path =
text !== ''
? `/Search?PageNumber=${page}&PageSize=${itemsPerPage}&SearchTerm=${text}`
: `?QueryLead=true&PageNumber=${page}&PageSize=${itemsPerPage}`;
return axios
.get(`${env_urls.api.entity}Entity${path}`, {
cancelToken: cts.token,
})
.then(response => {
return response;
})
.catch(error => {
if (
typeof cts !== 'undefined' &&
typeof cts.token !== 'undefined' &&
typeof cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
// request cancelled, everything ok
} else {
throw error;
}
});
})();
// response is undefined if the request has been cancelled
if (typeof response !== 'undefined') {
const data = response.data;
dispatch({ type: FETCH_ENTITIES_SUCCESS, data });
}
};
javascript beginner axios
New contributor
This piece of code is aimed to cancel any ongoing search requests sent from the web application if next request is started, e.g. after user updates the search term.
I tested it in the final application and everything looks fine. It has already been deployed to the remote environment in our organization.
It appears to work fine, but are there any pitfalls in the code?
const ongoingRequests = ;
const fetchData = (text, activePage, itemsPerPage) => async dispatch => {
dispatch({ type: FETCH_ENTITIES });
const page = activePage - 1;
let response;
// cancel any ongoing search requests
if (ongoingRequests.length > 0) {
ongoingRequests.map(x => x.cancel('next request started'));
ongoingRequests.length = 0;
}
// start a new request
response = await (async () => {
const cts = axios.CancelToken.source();
ongoingRequests.push(cts);
let path =
text !== ''
? `/Search?PageNumber=${page}&PageSize=${itemsPerPage}&SearchTerm=${text}`
: `?QueryLead=true&PageNumber=${page}&PageSize=${itemsPerPage}`;
return axios
.get(`${env_urls.api.entity}Entity${path}`, {
cancelToken: cts.token,
})
.then(response => {
return response;
})
.catch(error => {
if (
typeof cts !== 'undefined' &&
typeof cts.token !== 'undefined' &&
typeof cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
// request cancelled, everything ok
} else {
throw error;
}
});
})();
// response is undefined if the request has been cancelled
if (typeof response !== 'undefined') {
const data = response.data;
dispatch({ type: FETCH_ENTITIES_SUCCESS, data });
}
};
javascript beginner axios
javascript beginner axios
New contributor
New contributor
edited 2 days ago
Mast
7,43863686
7,43863686
New contributor
asked 2 days ago
michal
163
163
New contributor
New contributor
1
"Does it work as intended?" : it's up to you to tell us :]
– Calak
2 days ago
@Calak Everything is fine when I test it in the application but maybe there is an issue that I cannot see. Like some js construct that makes no sense ;)
– michal
2 days ago
"Does it work as intended?" Did you test it? One of the prerequisites of posting on Code Review is that the code has to work to the best of your knowledge. Does it? You say it works in the application. Is that the final application that needs it? A test application?
– Mast
2 days ago
1
@Mast I have updated the question.
– michal
2 days ago
1
When I implement this type of search functionality I usually cancel the current request when the user presses a key (on the keypress, paste events) and start a timer for a fraction of a second. I only start the request when the timer expires. That way I don't start requests while the user is still typing. I've also rarely found it unnecessary to keep an array of requests. I just keep the currently executing one if any.
– Marc Rohloff
2 days ago
add a comment |
1
"Does it work as intended?" : it's up to you to tell us :]
– Calak
2 days ago
@Calak Everything is fine when I test it in the application but maybe there is an issue that I cannot see. Like some js construct that makes no sense ;)
– michal
2 days ago
"Does it work as intended?" Did you test it? One of the prerequisites of posting on Code Review is that the code has to work to the best of your knowledge. Does it? You say it works in the application. Is that the final application that needs it? A test application?
– Mast
2 days ago
1
@Mast I have updated the question.
– michal
2 days ago
1
When I implement this type of search functionality I usually cancel the current request when the user presses a key (on the keypress, paste events) and start a timer for a fraction of a second. I only start the request when the timer expires. That way I don't start requests while the user is still typing. I've also rarely found it unnecessary to keep an array of requests. I just keep the currently executing one if any.
– Marc Rohloff
2 days ago
1
1
"Does it work as intended?" : it's up to you to tell us :]
– Calak
2 days ago
"Does it work as intended?" : it's up to you to tell us :]
– Calak
2 days ago
@Calak Everything is fine when I test it in the application but maybe there is an issue that I cannot see. Like some js construct that makes no sense ;)
– michal
2 days ago
@Calak Everything is fine when I test it in the application but maybe there is an issue that I cannot see. Like some js construct that makes no sense ;)
– michal
2 days ago
"Does it work as intended?" Did you test it? One of the prerequisites of posting on Code Review is that the code has to work to the best of your knowledge. Does it? You say it works in the application. Is that the final application that needs it? A test application?
– Mast
2 days ago
"Does it work as intended?" Did you test it? One of the prerequisites of posting on Code Review is that the code has to work to the best of your knowledge. Does it? You say it works in the application. Is that the final application that needs it? A test application?
– Mast
2 days ago
1
1
@Mast I have updated the question.
– michal
2 days ago
@Mast I have updated the question.
– michal
2 days ago
1
1
When I implement this type of search functionality I usually cancel the current request when the user presses a key (on the keypress, paste events) and start a timer for a fraction of a second. I only start the request when the timer expires. That way I don't start requests while the user is still typing. I've also rarely found it unnecessary to keep an array of requests. I just keep the currently executing one if any.
– Marc Rohloff
2 days ago
When I implement this type of search functionality I usually cancel the current request when the user presses a key (on the keypress, paste events) and start a timer for a fraction of a second. I only start the request when the timer expires. That way I don't start requests while the user is still typing. I've also rarely found it unnecessary to keep an array of requests. I just keep the currently executing one if any.
– Marc Rohloff
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can rewritte this piece of code:
.catch(error => {
if (
cts !== 'undefined' &&
cts.token !== 'undefined' &&
cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
} else {
throw error;
}
}
To:
.catch(error => {
if (
typeof cts === 'undefined' ||
typeof cts.token === 'undefined' ||
typeof cts.token.reason === 'undefined' ||
cts.token.reason.message !== 'next request started'
) {
throw error;
}
}
And you can go further into the simplification using this trick.
1
imo, you shouldn't usetypeof
you should just write it asif (cts === undefined) || ...
– Marc Rohloff
2 days ago
@MarcRohloff yeah, copy/paste's mistake :p Thx
– Calak
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can rewritte this piece of code:
.catch(error => {
if (
cts !== 'undefined' &&
cts.token !== 'undefined' &&
cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
} else {
throw error;
}
}
To:
.catch(error => {
if (
typeof cts === 'undefined' ||
typeof cts.token === 'undefined' ||
typeof cts.token.reason === 'undefined' ||
cts.token.reason.message !== 'next request started'
) {
throw error;
}
}
And you can go further into the simplification using this trick.
1
imo, you shouldn't usetypeof
you should just write it asif (cts === undefined) || ...
– Marc Rohloff
2 days ago
@MarcRohloff yeah, copy/paste's mistake :p Thx
– Calak
2 days ago
add a comment |
up vote
0
down vote
You can rewritte this piece of code:
.catch(error => {
if (
cts !== 'undefined' &&
cts.token !== 'undefined' &&
cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
} else {
throw error;
}
}
To:
.catch(error => {
if (
typeof cts === 'undefined' ||
typeof cts.token === 'undefined' ||
typeof cts.token.reason === 'undefined' ||
cts.token.reason.message !== 'next request started'
) {
throw error;
}
}
And you can go further into the simplification using this trick.
1
imo, you shouldn't usetypeof
you should just write it asif (cts === undefined) || ...
– Marc Rohloff
2 days ago
@MarcRohloff yeah, copy/paste's mistake :p Thx
– Calak
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
You can rewritte this piece of code:
.catch(error => {
if (
cts !== 'undefined' &&
cts.token !== 'undefined' &&
cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
} else {
throw error;
}
}
To:
.catch(error => {
if (
typeof cts === 'undefined' ||
typeof cts.token === 'undefined' ||
typeof cts.token.reason === 'undefined' ||
cts.token.reason.message !== 'next request started'
) {
throw error;
}
}
And you can go further into the simplification using this trick.
You can rewritte this piece of code:
.catch(error => {
if (
cts !== 'undefined' &&
cts.token !== 'undefined' &&
cts.token.reason !== 'undefined' &&
cts.token.reason.message === 'next request started'
) {
} else {
throw error;
}
}
To:
.catch(error => {
if (
typeof cts === 'undefined' ||
typeof cts.token === 'undefined' ||
typeof cts.token.reason === 'undefined' ||
cts.token.reason.message !== 'next request started'
) {
throw error;
}
}
And you can go further into the simplification using this trick.
edited 2 days ago
answered 2 days ago
Calak
1,939314
1,939314
1
imo, you shouldn't usetypeof
you should just write it asif (cts === undefined) || ...
– Marc Rohloff
2 days ago
@MarcRohloff yeah, copy/paste's mistake :p Thx
– Calak
2 days ago
add a comment |
1
imo, you shouldn't usetypeof
you should just write it asif (cts === undefined) || ...
– Marc Rohloff
2 days ago
@MarcRohloff yeah, copy/paste's mistake :p Thx
– Calak
2 days ago
1
1
imo, you shouldn't use
typeof
you should just write it as if (cts === undefined) || ...
– Marc Rohloff
2 days ago
imo, you shouldn't use
typeof
you should just write it as if (cts === undefined) || ...
– Marc Rohloff
2 days ago
@MarcRohloff yeah, copy/paste's mistake :p Thx
– Calak
2 days ago
@MarcRohloff yeah, copy/paste's mistake :p Thx
– Calak
2 days ago
add a comment |
michal is a new contributor. Be nice, and check out our Code of Conduct.
michal is a new contributor. Be nice, and check out our Code of Conduct.
michal is a new contributor. Be nice, and check out our Code of Conduct.
michal is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208275%2fcancelling-ongoing-http-requests-when-next-one-is-started-web-app-search-featu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"Does it work as intended?" : it's up to you to tell us :]
– Calak
2 days ago
@Calak Everything is fine when I test it in the application but maybe there is an issue that I cannot see. Like some js construct that makes no sense ;)
– michal
2 days ago
"Does it work as intended?" Did you test it? One of the prerequisites of posting on Code Review is that the code has to work to the best of your knowledge. Does it? You say it works in the application. Is that the final application that needs it? A test application?
– Mast
2 days ago
1
@Mast I have updated the question.
– michal
2 days ago
1
When I implement this type of search functionality I usually cancel the current request when the user presses a key (on the keypress, paste events) and start a timer for a fraction of a second. I only start the request when the timer expires. That way I don't start requests while the user is still typing. I've also rarely found it unnecessary to keep an array of requests. I just keep the currently executing one if any.
– Marc Rohloff
2 days ago