1

The code below is what I wrote.
I know 'await' keyword shouldn't be in for-loop well.

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);

So, I tried like this but the 'result' contains every elements from 'userBooks' and that's not what I wanted.

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 help this poor newbie

  • filter your result after `Promise.all()` based on some criteria. – Rahul Kumar Jul 23 '21 at 08:02
  • what result you are getting? and what do you expect? – Rahul Kumar Jul 23 '21 at 08:02
  • *"that's not what I wanted."*: it is not clear what you *do* want. Are you looking for: `const result = (await Promise.all(booksNotBackedUp)).filter(files => !files.length);`? – trincot Jul 23 '21 at 08:07
  • 3
    @Braks, no, that is an anti-pattern. `getFiles` already returns the promise. No need to create another one. – trincot Jul 23 '21 at 08:09
  • Why would that be an anti-pattern? Async functions implicitly return promises too, what's the difference? and he is not even calling an async function in his 2nd example. In his first he's just looping through them and actually calling them instead of pushing them to his array for the call... Do you have any info where I could read up on it being an anti-pattern? First time I heard that Edit: trincot is right, i'm wrong ^^ – Braks Jul 23 '21 at 08:11
  • 2
    @Braks: [What is the explicit promise construction antipattern and how do I avoid it?](https://stackoverflow.com/q/23803743/5459839). The Asker *is* calling an async function in both code blocks (`getFiles`). – trincot Jul 23 '21 at 08:13
  • Is it not possible to write a query that just returns an array of books that aren't backed up? – Andy Jul 23 '21 at 08:22
  • Thank you for all the comments! You guys are awesome! userBooks: Users have their own books and its IDs are like this "0, 1, 2, ..." booksNotBackedUp: And then I want to find Ids of empty books like this "2, 3, ..." @trincot – A Developer Jul 23 '21 at 08:29
  • Unfortunately, I didn't make the column if the books are backed up or not. I want to know if it was or not by communicating(?) with the cloud at this moment. Thank you. @Andy – A Developer Jul 23 '21 at 08:37

1 Answers1

0

Your condition to add the promise into array lies inside the promise result. It is not possible to add the promise in array beforehand as promise is not resolved yet.

You can do the filtering after promise.all() resolved. In that case you will have all the results and you can use array's filter method.

Shivaji Mutkule
  • 1,020
  • 1
  • 15
  • 28