AniUI Academy
medium+250 XPPractice

Filter an array with an async predicate

Filtering a list of documents by whether the current user may see each one means asking the server per document. You want those checks to run together, but the surviving items must still appear in their original order.

Implement filterAsync(items, predicate). Call predicate(item, index) for every item, starting all of the calls before awaiting any of them. Resolve with a new array containing the items whose awaited result is truthy, in their original order. An empty array resolves with an empty array, and the input array is never mutated. If any predicate rejects, reject with the first rejection reason.

What it has to do

  • Start every predicate call before awaiting the results.
  • Keep the surviving items in their original order.
  • Pass the index to the predicate as the second argument.
  • Do not mutate the input array.
  • Reject with the first rejection reason if a predicate fails.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.