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










share|improve this question
























  • 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

















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










share|improve this question
























  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




















  • 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


















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














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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        estus

        63.2k2193200




        63.2k2193200






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Cypress Hill

            what are some tips for doing well in the interview? [on hold]

            How does a super-power salesman not get shut down for legal reasons?