I've used JQuery in the past to do AJAX requests. I am trying to now use fetch and I am struggling.
My code is very simple, and it calls a C# backend. The back end break point is hit, so I don't believe there is any issue with the URL
this.get = function (url) {
return getData(url);
};
async function getData(url = '') {
const response = await fetch(url, {
method: 'GET', // *GET, POST, PUT, DELETE, etc.
mode: 'same-origin', // no-cors, *cors, same-origin
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow',
referrerPolicy: 'no-referrer'
});
return await response.json(); // BREAK POINT NEVER HIT
}
In the browsers dev tools, I put a break point at the line const response = await fetch(url, { and another on the return await response.json;
The first break is hit. The second never hits. I don't see any error message or exception.
How do I find out what is going on?
Looking at the response, it shows it's pending a promise
EDIT
It seems as if my program is not waiting with await... My understanding is the code should not progress until it has received (waited) for a response. What is happening is, as soon as it hits the await, it seems to exit the function. The breakpoint on return await response.json() does eventually get hit, but await is acting like a new thread.
