0

I am somewhat new to the latest Javascript syntax and I am building an application that relies on it. I was able to create an async function that incorporates the fetch command to go to my Java back-end and retreive data. Though, I want to have the ability to set a timeout value of like 3 minutes, and if it goes over that to display a message. I somewhat know how to do it in plain vanilla javascript but I can't seem to get it for the async function. I read some examples online and documentation but still can't figure it out.

Async Function:

async function deleteGP(code){

                const csrfToken = $("meta[name='_csrf']").attr("content");
                console.log(csrfToken);


                const result = await fetch ('/ajax/deleteGrouping' , {

                    method: "POST",


                    body: JSON.stringify ({
                        dtoTiername: code
                    }),             

                    headers : {
                        "X-CSRF-Token": csrfToken,
                        "Content-type": "application/json; charset=UTF-8",
                        'Accept': 'application/json'

                    },                  


                });

                const data = await result.json();
                console.log(data);

                if(data){
                    $('#overlay').hide();
                    $('#successModal').modal();
                    $('#successModal').on('hidden.bs.modal', function () {
                        location.reload(true);
                    });
                }else {
                    $('#overlay').hide();
                    $('#failModal').modal();
                    $('#failModal').on('hidden.bs.modal', function () {
                        location.reload(true);
                    });
                }

            }   
kane_004
  • 253
  • 3
  • 18
  • 1
    Does this answer your question? [What is the best general practice to timeout a function in promise](https://stackoverflow.com/q/30936824) – palaѕн May 27 '20 at 13:17
  • @palaѕн So The answer you posted seems clear cut but it is for a promise, how would i do it for my async call? Do I wrap the function in the timeout? Do I make the timeout a parameter and do it that way? Still not sure how to use it with async. Thanks – kane_004 May 27 '20 at 13:38

0 Answers0