I have an function for each await and it gives a string and adds it to an array, but the log that I got later shows the array is empty and it is done before the for each is done.
const paths: Array<string> = [];
await filterFiles.forEach((data, index) => {
fileSave(data, `${name}@${index}`, National_Code, "medium", "image")
.then((data: string) => {
paths.push(data);
console.log(paths);
console.log("log 2");
})
.catch((err) => {
res.status(500).json("There is a problem saving the files");
return err;
});
});
console.log(paths);
console.log("log 1");
Result :
[]
log 1
[ 'image/Czech@0_0927735016.jpeg' ]
log 2
[ 'image/Czech@0_0927735016.jpeg', 'image/Czech@1_0927735016.jpeg' ]
log 2
What did I do wrong?