Synchronic code with map functions

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Synchronic code with map functions



I think I got totally lost, I thought I had at least some grasp on async stuff, but this particular case got me confused.



I perform a search on the database and expect an array of results.
Then i map through the array, check for flag and push info to the array.



However, I'm doing async calls wrong, because when I console log the finalData it's always empty, therefore it resolves before it should.



Here are my attempts, tried it both ways:


let finalData = ;

let lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() - 7);

db.find( createdAt: "$gte": lastWeek ).then((records, err) =>

async function test()
let array = _.map(records, (record) =>
db2.find( 'foo': record.id ).then((bar, err) =>

if (bar.endDate)
let data = `Email: $record.email`;
finalData.push(data);
else
let data = `Email: $record.email`;
finalData.push(data);

);
);

await Promise.all(array).then(() =>
console.log(finalData);
);



test();
);



and:


db.find( createdAt: "$gte": lastWeek ).then((records, err) =>

let promise1 = new Promise((resolve, reject) =>
_.map(records, (record) =>
db2.find( 'foo': record.id ).then((bar, err) =>

if (bar.endDate)
let data = `Email: $record.email`;
finalData.push(data);
else
let data = `Email: $record.email`;
finalData.push(data);

);
);

resolve();
);

promise1.then(() =>
console.log(finalData);
)
);



Results are the same, I'm doing something wrong.





Just a note on map ... in this case you should use forEach or simple for loop since there is no real need for map if you do not return anything from it.
– Akrion
Aug 6 at 16:00



map


forEach


for loop


map





db.find() is async. You need to await on it.
– marekful
Aug 6 at 16:02





Ah, that is correct, initially I intended to return something but ended up not doing it, will change that, thanks
– MazMat
Aug 6 at 16:02





@Akrion You cannot use forEach with asynchronous functions
– Bergi
Aug 6 at 16:04


forEach





@Bergi good call. I was thinking something among the lines of for..of.
– Akrion
Aug 6 at 16:10


for..of




1 Answer
1


return


map


array


undefined


finalData


push


Promise.all


then


err


then


async


await




let lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() - 7);

const records = await db.find( createdAt: "$gte": lastWeek );
let promises = records.map(async (record) =>
const bar = await db2.find( 'foo': record.id );
if (bar.endDate)
return `Email: $record.email`;
else
return `Email: $record.email`;

);

const finalData = await Promise.all(promises)
console.log(finalData);



Or alternatively:




let lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() - 7);

return db.find( createdAt: "$gte": lastWeek ).then(records =>
let promises = records.map(record =>
return db2.find( 'foo': record.id ).then(bar =>
// ^^^^^^
if (bar.endDate)
return `Email: $record.email`;
// ^^^^^^
else
return `Email: $record.email`;
// ^^^^^^

);
);
return Promise.all(promises);
// ^^^^^^
).then(finalData =>
console.log(finalData);
);





That looks like something that actually might work, when I'm back on PC I'll check this solution out and accept the answer if everything goes smoothly. Thank you!
– MazMat
Aug 6 at 16:32






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard