Here is the code snippet that I have written:
I am aware that using the 'await' keyword inside a for-loop is not recommended.
const booksNotBackedUp: number[] = [];
for (let i = 0; i < usersBooks.length; i += 1) {
const files = await Util.file.getFiles(Util.file.DATA_BUCKET, { prefix: `${userId}/data/${usersBooks[i]}/sqlite` });
if (files.length === 0) booksNotBackedUp.push(usersBooks[i]);
}
console.log('booksNotBackedUp', booksNotBackedUp);
I attempted to refactor the code in the following way, however the 'result' variable ended up containing all elements from 'userBooks', which was not my desired outcome.
const booksNotBackedUp: any[] = [];
userBooks.forEach((book) => {
booksNotBackedUp.push(Util.file.getFiles(Util.file.DATA_BUCKET, { prefix: `${userId}/data/${book}/sqlite` })
})
const result = await Promise.all(booksNotBackedUp);
console.log('booksNotBackedUp', result);
Please provide some guidance to help out this struggling newcomer😥