So I'm trying to de-jquery my code. Currently I use a jquery ajax function tith the parameter "async" set to false.
That looks something like this
$.ajax({
...
async: false,
...
success:function(data) {
list = JSON.parse(data);
},
error:function(data){
console.log(data);
}
});
I now tried to follow the lovely moz-docs on how to fetch the data in vanilla js, but everything seems to be asynchronous, which breaks the rest of my code.
How can I fetch the data from my (local) API synchrounously?
This is my current (not working) approach
function postData(url = '', data = {}) {
// Default options are marked with *
const response = fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *client
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
}
var einzelhandel = [];
postData('src/php/api/getDataEinzelhandel', {})
.then((data) => {
einzelhandel = data;
});