I'm trying to use async fetch call to get data from the server but the result looks like an empty async function. what am I doing wrong?
this is my code
function initAutoComplete (selector, params = []) {
let config = {
selector: selector,
data: {
src: async () => {
try {
let myurl = `/component/get_states?country_id=64`; // http://localhost
const response = await fetch(myurl)
const source = await response.json();
console.log('source', source); // ==> this doesn't even appear in the console log
return source;
} catch (err) {
return err;
}
},
keys: ["state_name"],
cache: true,
},
};
console.log('config', config.data);
return config;
}
const asd = initAutoComplete('#state_name');
console.log('asd', asd.data);
this is the result in the developer tools

although when I used postman to test the url, it shows the data as expected
[
{
"id": "17",
"state_name": "Bali",
"country_name": "Indonesia"
},
{
"id": "16",
"state_name": "Banten",
"country_name": "Indonesia"
},
{
"id": "7",
"state_name": "Bengkulu",
"country_name": "Indonesia"
},
]
initAutoComplete(), but all that does is define aconfigobject that contains adata.src()function, but nothing you show ever calls that function. So, per this code that function is never executed and never should be. The function has been defined, but not called. You would useasd.data.src().then(...).catch(...)to call it.