AniUI Academy
medium+250 XPPractice

Try each source until one works

Reading a setting might mean trying an in-memory value, then a local snapshot, then the network. Each attempt is a different function, they must be tried in order, and the expensive ones must not run if an earlier one succeeds.

Implement firstSuccessful(tasks), where tasks is an array of zero-argument functions returning promises. Call them one at a time in order and resolve with the result of the first that fulfils, without calling the remaining tasks. If a task rejects, move on to the next one. If every task rejects, reject with the error from the last task. If tasks is empty, reject with new Error("No tasks provided").

What it has to do

  • Call the tasks one at a time, in order.
  • Resolve with the first fulfilled value and skip the remaining tasks.
  • Continue past a rejecting task.
  • Reject with the last task's error when they all reject.
  • Reject with an Error reading No tasks provided for an empty array.

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.