I have a bit of a problem with my callback function. The flow of the code looks like this:
- Call queryDB()
- QueryDB gets data from an API and invokes the callback
- Callback returns data
- Print data on screen
//index.js
console.log(dscAPI.queryDB(dscDB, 'recent'));
and the functions
function getRecent(dscDB, callback) {
const query = 'year=' + new Date().getFullYear();;
const params = {
type: 'master',
per_page: '10'
}
dscDB.search(query, params)
.then(function(data) {
callback(data.results);
})
.catch((err) => console.log(err));
}
function returnRecent(data) {
console.log(data); //returns desired output
return data; //returns undefined
}
module.exports = {
queryDB: function(dscDB) {
getRecent(dscDB, returnRecent);
}
}
The issue is: callback returns undefined, even before console.log(). I have been trying using async/await to make it wait for the value, but it didn't seem to be able to do that. I read this article: https://github.com/maxogden/art-of-node#callbacks, and I structured my code similar way, but to no avail. Maybe you have any ideas how should I handle that?