I am really getting a headache trying to make use of the async/await functionality when using for loops. I am using Node.js version: v8.6.0
In short, I am trying to retrieve many rows from a database and then push them all to an array and return that array.
I have successfully done this using callbacks but can't figure out how to do it with async/await.
My current code using callbacks that works
function main(db) {
gatherDates(db, function(dates) {
console.log(dates); //successful
});
}
function gatherDates(db, callback) {
const dates = [];
let today = getToday();
dates.push(today);
let dateQuery = "SELECT date FROM event_dates";
db.query(dateQuery, (err, newDates) => {
for(let row of newDates) {
dates.push(row.date);
}
callback(dates);
});
}
The code that fails trying to use async/await
async function main(db) {
let dates = await gatherDates(db);
console.log(dates); //undefined or not all of the data
}
function gatherDates(db) {
const dates = [];
let today = getToday();
dates.push(today);
let dateQuery = "SELECT date FROM event_dates";
db.query(dateQuery, (err, newDates) => {
for(let row of newDates) {
dates.push(row.date);
}
return Promise.resolve(dates);
});
}
I have googled trying to find a solution, I have tried using multiple promise and then calling return Promise.all(promises);
at the end but it did not work. I have tried return new Promise((resolve, reject)=>resolve(dates))};
. I have looked at Promise and async/await tutorials and examples which usually work for me but when it comes to looping through data that is where I am having an issue. I know there is something fundamental that I am missing so any help is appreciated. Thank you!