I am constructing a demo that imitates the structure of requesting/receiving data from a database. The output should just be a console log of the returned array.
I am getting the message Uncaught TypeError: func_get_db_data is not a function
I saw this question TypeError: #<Promise> is not a function
But the answer is a little too specific for me to apply to my situation. How can I understand the solution here?
let func_get_db_data = new Promise((resolve, reject) => {
resolve([
{name: 'Bob', gender:'m', class:'junior', job: 'Teacher'},
{name: 'Dan', gender:'m', class:'senior', job: 'Detective'},
{name: 'Ann', gender:'f', class:'junior', job: 'Analyst'},
{name: 'Sue', gender:'f', class:'retired', job: 'Accountant'}
]);
});
const tableData = func_get_db_data().then( (data) => {
return data;
});
console.log(tableData);
Update
Based on below comments, I tried this (but didn't quite get it... it just returns an empty object)
let func_get_db_data = () => {
return [
{name: 'Bob', gender:'m', class:'junior', job: 'Teacher'},
{name: 'Dan', gender:'m', class:'senior', job: 'Detective'},
{name: 'Ann', gender:'f', class:'junior', job: 'Analyst'},
{name: 'Sue', gender:'f', class:'retired', job: 'Accountant'}
];
};
const getTables = new Promise(func_get_db_data);
getTables.then( (data) => {
return data;
});
console.log(getTables);
What I would really like is to structure the code so that a structure such as this returns the data:
const tableData = func_get_db_data().then( (data) => {
return data;
});