Cancel request if there is new one(promises)
up vote
1
down vote
favorite
I send request to server everytime user types something. I use debounce for the 400ms delay:
type = debounce((text) => {
this.props.actions.loadInfo(text)
}, 400);
When I type something, stop and start again and repeat it, several requests are send and I receive irrelevant data. I use promises:
export const loadInfo = (text) => dispatch => {
loadData(text).then(result => {
dispatch(showUserData(result));
});
};
export const loadData = async (text) => {
const tabData = await axios.get(`url&query=${text}`);
return tabData;
}
I need somehow cancel previous request if user sends the new one(when he typed something), what is the best way to do that? I expected debounce will help me but not. I use axios. This is not duplicate of questions here, I checked provided solutions but thet don't help me
javascript promise axios cancellation
|
show 14 more comments
up vote
1
down vote
favorite
I send request to server everytime user types something. I use debounce for the 400ms delay:
type = debounce((text) => {
this.props.actions.loadInfo(text)
}, 400);
When I type something, stop and start again and repeat it, several requests are send and I receive irrelevant data. I use promises:
export const loadInfo = (text) => dispatch => {
loadData(text).then(result => {
dispatch(showUserData(result));
});
};
export const loadData = async (text) => {
const tabData = await axios.get(`url&query=${text}`);
return tabData;
}
I need somehow cancel previous request if user sends the new one(when he typed something), what is the best way to do that? I expected debounce will help me but not. I use axios. This is not duplicate of questions here, I checked provided solutions but thet don't help me
javascript promise axios cancellation
stackoverflow.com/a/26735236/2630817
– Just code
yesterday
1) I use axios 2) that won't work as I tried in other case that variant
– rick1
yesterday
again, there is no correct answer
– rick1
yesterday
@rick1 What do you mean by 'correct'? I believe my own answer is correct one, otherwise I wouldn't link it.
– estus
yesterday
1
In case this is solved by debouncingloadData, it should be something like 0bin.net/paste/… . Hope this helps. Also noticed that demo was wrong in the post I linked, fixed it, stackblitz.com/edit/react-ecsbrz . Also was wrong link in this comment, updated it, sorry for that.
– estus
yesterday
|
show 14 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I send request to server everytime user types something. I use debounce for the 400ms delay:
type = debounce((text) => {
this.props.actions.loadInfo(text)
}, 400);
When I type something, stop and start again and repeat it, several requests are send and I receive irrelevant data. I use promises:
export const loadInfo = (text) => dispatch => {
loadData(text).then(result => {
dispatch(showUserData(result));
});
};
export const loadData = async (text) => {
const tabData = await axios.get(`url&query=${text}`);
return tabData;
}
I need somehow cancel previous request if user sends the new one(when he typed something), what is the best way to do that? I expected debounce will help me but not. I use axios. This is not duplicate of questions here, I checked provided solutions but thet don't help me
javascript promise axios cancellation
I send request to server everytime user types something. I use debounce for the 400ms delay:
type = debounce((text) => {
this.props.actions.loadInfo(text)
}, 400);
When I type something, stop and start again and repeat it, several requests are send and I receive irrelevant data. I use promises:
export const loadInfo = (text) => dispatch => {
loadData(text).then(result => {
dispatch(showUserData(result));
});
};
export const loadData = async (text) => {
const tabData = await axios.get(`url&query=${text}`);
return tabData;
}
I need somehow cancel previous request if user sends the new one(when he typed something), what is the best way to do that? I expected debounce will help me but not. I use axios. This is not duplicate of questions here, I checked provided solutions but thet don't help me
javascript promise axios cancellation
javascript promise axios cancellation
edited yesterday
asked yesterday
rick1
1329
1329
stackoverflow.com/a/26735236/2630817
– Just code
yesterday
1) I use axios 2) that won't work as I tried in other case that variant
– rick1
yesterday
again, there is no correct answer
– rick1
yesterday
@rick1 What do you mean by 'correct'? I believe my own answer is correct one, otherwise I wouldn't link it.
– estus
yesterday
1
In case this is solved by debouncingloadData, it should be something like 0bin.net/paste/… . Hope this helps. Also noticed that demo was wrong in the post I linked, fixed it, stackblitz.com/edit/react-ecsbrz . Also was wrong link in this comment, updated it, sorry for that.
– estus
yesterday
|
show 14 more comments
stackoverflow.com/a/26735236/2630817
– Just code
yesterday
1) I use axios 2) that won't work as I tried in other case that variant
– rick1
yesterday
again, there is no correct answer
– rick1
yesterday
@rick1 What do you mean by 'correct'? I believe my own answer is correct one, otherwise I wouldn't link it.
– estus
yesterday
1
In case this is solved by debouncingloadData, it should be something like 0bin.net/paste/… . Hope this helps. Also noticed that demo was wrong in the post I linked, fixed it, stackblitz.com/edit/react-ecsbrz . Also was wrong link in this comment, updated it, sorry for that.
– estus
yesterday
stackoverflow.com/a/26735236/2630817
– Just code
yesterday
stackoverflow.com/a/26735236/2630817
– Just code
yesterday
1) I use axios 2) that won't work as I tried in other case that variant
– rick1
yesterday
1) I use axios 2) that won't work as I tried in other case that variant
– rick1
yesterday
again, there is no correct answer
– rick1
yesterday
again, there is no correct answer
– rick1
yesterday
@rick1 What do you mean by 'correct'? I believe my own answer is correct one, otherwise I wouldn't link it.
– estus
yesterday
@rick1 What do you mean by 'correct'? I believe my own answer is correct one, otherwise I wouldn't link it.
– estus
yesterday
1
1
In case this is solved by debouncing
loadData, it should be something like 0bin.net/paste/… . Hope this helps. Also noticed that demo was wrong in the post I linked, fixed it, stackblitz.com/edit/react-ecsbrz . Also was wrong link in this comment, updated it, sorry for that.– estus
yesterday
In case this is solved by debouncing
loadData, it should be something like 0bin.net/paste/… . Hope this helps. Also noticed that demo was wrong in the post I linked, fixed it, stackblitz.com/edit/react-ecsbrz . Also was wrong link in this comment, updated it, sorry for that.– estus
yesterday
|
show 14 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
The problem is similar to this one. Axios cancellation API can be used to cancel old requests. This should be done in a function that does a request (loadData) and has direct access to Axios, it may be also debounced:
let cancelObj;
export const loadData = debounce((text) => {
if (cancelObj) {
this.cancelObj.cancel();
}
cancelObj = CancelToken.source();
return axios.get(`url&query=${text}`, {
cancelToken: this._fetchDataCancellation.token
}).catch(err => {
// request wasn't cancelled
if (!axios.isCancel(err))
throw err;
});
}, 200);
Since Redux is used, other solutions may involve it, they depend on how Redux is used.
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
The problem is similar to this one. Axios cancellation API can be used to cancel old requests. This should be done in a function that does a request (loadData) and has direct access to Axios, it may be also debounced:
let cancelObj;
export const loadData = debounce((text) => {
if (cancelObj) {
this.cancelObj.cancel();
}
cancelObj = CancelToken.source();
return axios.get(`url&query=${text}`, {
cancelToken: this._fetchDataCancellation.token
}).catch(err => {
// request wasn't cancelled
if (!axios.isCancel(err))
throw err;
});
}, 200);
Since Redux is used, other solutions may involve it, they depend on how Redux is used.
add a comment |
up vote
0
down vote
The problem is similar to this one. Axios cancellation API can be used to cancel old requests. This should be done in a function that does a request (loadData) and has direct access to Axios, it may be also debounced:
let cancelObj;
export const loadData = debounce((text) => {
if (cancelObj) {
this.cancelObj.cancel();
}
cancelObj = CancelToken.source();
return axios.get(`url&query=${text}`, {
cancelToken: this._fetchDataCancellation.token
}).catch(err => {
// request wasn't cancelled
if (!axios.isCancel(err))
throw err;
});
}, 200);
Since Redux is used, other solutions may involve it, they depend on how Redux is used.
add a comment |
up vote
0
down vote
up vote
0
down vote
The problem is similar to this one. Axios cancellation API can be used to cancel old requests. This should be done in a function that does a request (loadData) and has direct access to Axios, it may be also debounced:
let cancelObj;
export const loadData = debounce((text) => {
if (cancelObj) {
this.cancelObj.cancel();
}
cancelObj = CancelToken.source();
return axios.get(`url&query=${text}`, {
cancelToken: this._fetchDataCancellation.token
}).catch(err => {
// request wasn't cancelled
if (!axios.isCancel(err))
throw err;
});
}, 200);
Since Redux is used, other solutions may involve it, they depend on how Redux is used.
The problem is similar to this one. Axios cancellation API can be used to cancel old requests. This should be done in a function that does a request (loadData) and has direct access to Axios, it may be also debounced:
let cancelObj;
export const loadData = debounce((text) => {
if (cancelObj) {
this.cancelObj.cancel();
}
cancelObj = CancelToken.source();
return axios.get(`url&query=${text}`, {
cancelToken: this._fetchDataCancellation.token
}).catch(err => {
// request wasn't cancelled
if (!axios.isCancel(err))
throw err;
});
}, 200);
Since Redux is used, other solutions may involve it, they depend on how Redux is used.
answered 8 hours ago
estus
63.2k2193200
63.2k2193200
add a comment |
add a comment |
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%2fstackoverflow.com%2fquestions%2f53371973%2fcancel-request-if-there-is-new-onepromises%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
stackoverflow.com/a/26735236/2630817
– Just code
yesterday
1) I use axios 2) that won't work as I tried in other case that variant
– rick1
yesterday
again, there is no correct answer
– rick1
yesterday
@rick1 What do you mean by 'correct'? I believe my own answer is correct one, otherwise I wouldn't link it.
– estus
yesterday
1
In case this is solved by debouncing
loadData, it should be something like 0bin.net/paste/… . Hope this helps. Also noticed that demo was wrong in the post I linked, fixed it, stackblitz.com/edit/react-ecsbrz . Also was wrong link in this comment, updated it, sorry for that.– estus
yesterday