tezvyn:

Fetch user profiles concurrently and handle individual failures

AI-drafted, machine-checkedSource: api.dart.devintermediate

Tests Future.wait concurrency and per-future error isolation. Answer: map IDs to fetchProfile, pass to Future.wait. For partial failures, attach catchError per future to return null, then filter. Red flag: try-catch around Future.wait or sequential awaits.

WHAT THIS TESTS: Your grasp of Dart concurrency primitives and error boundaries. The interviewer wants to see that you know Future.wait launches futures in parallel and collects results, but also that you understand its error semantics: by default, one failure poisons the whole list. The second part checks whether you can isolate failures to individual requests without losing the successes.

A GOOD ANSWER COVERS: First, concurrency via Future.wait. You map the list of IDs to fetchProfile calls immediately, because futures start running when created. Passing that iterable to Future.wait executes them concurrently and returns a Future of List of UserProfile. Second, the default failure mode. If any fetchProfile throws, Future.wait completes with that error and discards other errors; eagerError controls timing but not the all-or-nothing behavior. Third, the fix for individual failures. Before passing the futures to wait, attach error handling to each one individually, for example using catchError returning null or using a try-catch inside an async map block. This guarantees every future in the list resolves successfully, so Future.wait always yields a full list. Fourth, result sanitization. After awaiting, filter out the nulls or sentinels so the caller gets only valid profiles. Optionally mention that if you need error details, you can return a Result type instead of null.

COMMON WRONG ANSWERS: Awaiting each fetch inside a for-loop sequentially. That gives correct behavior but destroys concurrency and is a red flag for a senior role. Wrapping the entire Future.wait call in a try-catch without per-future error handlers; this catches the error but loses all successful profiles that had already completed. Using eagerError set to true thinking it solves partial failure; it only makes the failure faster, it still throws away the batch. Returning Stream or Isolate for a simple network fan-out; over-engineering for a bounded list of HTTP requests.

LIKELY FOLLOW-UPS: How would you limit concurrency if the list has ten thousand IDs? Answer: use a pool or chunk with Future.wait on batches, or a semaphore. What if the API has a rate limit? Answer: add throttling or backoff. How would you cache results? Answer: memoize fetchProfile or use a local cache layer. Would you use Future.wait in a Flutter build method? Answer: no, use FutureBuilder or a state management solution.

ONE CONCRETE EXAMPLE: For the failure-tolerant version, map each id to fetchProfile id then catchError returning null, collect to a list, then await Future.wait on that list. After awaiting, filter with whereType to drop nulls. This preserves concurrency, prevents total failure, and keeps the return type clean.

Read the original → api.dart.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.