I have a problem with async - await
.
What is happening?
I have a function of callback that a sends an array of objects, with valid objects and invalid objects to other function.
In the function, I do the validation, if it is valid, and do a push
to save it in an array.
And I want to pass it to another function JUST when it finalizes with all objects valid. But it passes one per one and not all.
Someone could help me ?
Function that I call the callback passing the array of objects valid or not:
const getNewNotification = async (callback) => {
await pool.query('SELECT * from alerts', (err, res) => {
if (err) {
console.log(err)
} else {
const newsNotification = res.rows;
callback(newsNotification)
}
})
}
Function that I make the push
:
const sendNotification = async (notification, callback) => {
let domain;
let subdomain;
let name;
let userHistory;
let userDescription;
let sequenceHistory;
let personYes = [];
let person;
await notification.map(async (user) => {
// VERIFY IF THE OBJECT IS VALID
await pool.query(`SELECT * from wfm where mail = '${user.id}''`, async (err, res) => {
if (res.rows.length !== 0) {
person = {
domain: res.rows[0].domain_name,
subdomain: res.rows[0].subdomain_name,
name: res.rows[0].name,
userHistory: user.id,
userDescription: user.change_description,
sequenceHistory: user.sequence,
}
// MAKE THE PUSH...
await personYes.push(person);
callback2(personYes);
}
Basically, i want that the function sendNotification
send the data JUST when it to end, how i make it?